Search code examples
symfonyshopware6

How to programatically change the routing to add my own "filter" by keyword


My case is the following: I have a Shopware Bundle and i need to change/extend the routing in such a way that if a request URL contains a keyword "xyz", the request is forwarded to a controller in my bundle without checking further if the route is available in "static routes" for example.

For instance: "/xyz/1/lorem/3" or "/xyz/5/3/ipsum" etc. all need to be rerouted to the controller in my bundle, since they contain the keyword /xyz.

Is there a service i can overwrite/decorate or something similar where i can implement this behaviour?


Solution

  • You can have a placeholder in your route with a default and a requirement allowing for all characters:

    /**
     * @Route("/xyz{anything}", name="frontend.my.action", methods={"GET"}, defaults={"anything"=""}, requirements={"anything"=".+"})
     */
    public function myAction(Request $request): Response
    {
        $anything = $request->get('anything');
        // ...
    }
    

    This will match any url starting with /xyz and every set of characters that follows afterwards is considered to be part of anything.