Search code examples
symfonyroutessymfony-3.3symfony-routingsymfony3.x

Symfony 3 - route requirements - all URL-parts mapped to parameter 1 when defining a route requirement


I am working on a project with Symfony 3.3.10.

I defined a route with 3 parameters like this

/**
 * @Route("/{_locale}/{cat_1}/{cat_2}/{cat_3}", name="cat_site",
 *     defaults={"cat_1" = "", "cat_2" = "", "cat_3" = ""})
 */
public function catSiteAction(Request $request, $cat_1, $cat_2, $cat_3)
{ ... }

Calling the URL

[root_path]/en/x/y/z

resulted (as expected) in the method-parameters to be set to

cat_1 = 'x'
cat_2 = 'y'
cat_3 = 'z'

which ist exactly what I want.

Now I configured FOSUserBundle and for the bundles routes to work I added a requirement to the route definition to not trigger the route if cat_1 is set to 'login' or 'logout':

/**
 * @Route("/{_locale}/{cat_1}/{cat_2}/{cat_3}", name="standard_site",
 *     defaults={"cat_1" = "", "cat_2" = "", "cat_3" = ""},
 *     requirements={"cat_1": "^(?!login|logout).+"})
 */
public function catSiteAction(Request $request, $cat_1, $cat_2, $cat_3)
{ ... }

When calling the same url as before

[root_path]/en/x/y/z

this leads to a parameter setting like this:

cat_1 = 'x/y/z'
cat_2 = ''
cat_3 = ''

Obviously this is not what I intend the parameters to be and I have no clue at all why this is happening. I can't see anything in the requirements definitions or in the used regex which can cause this.

Any ideas are highly appreciated.


Solution

  • The issue is you are modifying the default regular expression: [^/]+ for the cat_1 parameter:

    Symfony doc

    Try the following route not allowing the / to be present (edited):

    requirements={"cat_1": "^(?!login|logout)[^\/]+"})