-->

Locale in route prefix

2019-08-05 06:47发布

问题:

I'm trying to prefix all routes within single controller with _locale but so far I haven't had any success:

/**
 * @Route("/{_locale}/events", requirements={"_locale": "en"})
 * @Route("/{_locale}/evenements", requirements={"_locale": "fr"})
 */
class SomeController extends Controller{

    /**
     * @Route("/", name="events")
     */
    public function indexAction(){
       ... 
    }

    ...
}

route:debug does not even list URLs prefixed by second @Route. I tried removing _locale slug altogether but still, route could not be found.

Am I missing something here? Is it even possible to have multiple prefixes?

Any help would be greatly appreciated...

回答1:

Firstly, do you have defined in your file Ressources/config/routing.yml that you're using annotation for this controller ?

#file Ressources/config/routing.yml
blog:
    resource: "@YourBundle/Controller"
    type:     annotation

Also you can define your routes as :

/**
 * @Route("/en/events", defaults={"_locale": "en"})
 * @Route("/fr/evenements", defaults={"_locale": "fr"})
 */