Search code examples
symfonyroutessymfony-3.4symfony3.xsymfony-routing

Symfony Routing Requirements ignored


In a Symfony 3.4 Application I have the following route definition

/**
 * @Route("/{_locale}", name="homepage")
 *     requirements={
 *         "_locale":"de"
 *     }
 */
public function indexAction( Request $request, $_locale )
{ ... }

When calling the url

[base]/en

it still just routes into the route ignoring the requirement of _locale to be 'de' only. Switching the ":" in the requirement statement to "=" doesn't help.

The only other route definition I have so far is

/**
 * @Route("/", name="base")
 */
public function baseAction(Request $request)
{
    return $this->redirectToRoute( 'homepage', array('_locale' => 'de') );
}

Any ideas are very welcome.


Solution

  • As you can see here, the "requirement" part of the Route annotation should be within the Route annotation itself, so inside of the parenthesis. The way you wrote it, it's not considered part of an annotation.

    Try

    /**
     * @Route("/{_locale}", name="homepage",
     *     requirements={
     *         "_locale": "de"
     *     })
     */
    public function indexAction( Request $request, $_locale )
    { ... }
    

    Edit: Maybe this example from the Symfony 3.4 doc is better than the link I initially posted. Notice how the Route annotation started line 7 is closed by the parenthesis line 15.