BadHorsie
BadHorsie

Reputation: 14564

Symfony2 - What is the point of specifying your own route names?

Currently learning Symfony. Just wondering about route names, which the book doesn't really seem to say in advance what they are for.

As I understand it, @Route annotations are given a default name based on the bundle, controller and action name. So what's the point in specifying your own route names?

Upvotes: 2

Views: 709

Answers (1)

Cerad
Cerad

Reputation: 48873

Route names are important because your code will often need to build url's. The route name is how you specify which url to construct. Names are also handy for listeners (such as authentication) which process specific url's.

  1. Annotation generated default names are fine but tend to be long and could change on you.

  2. Yes, I would give every route a custom name for convenience and readability. Plus, I don't use annotations. To me at least, storing routes in a central file make the code easier to maintain. It avoids the need to search through multiple controller files trying to determine which code handles which request.

  3. Routes with the same name will replace any previously loaded route. It's useful if you wish to override a route from a third party bundle.

  4. Namespace your route by using some form of your bundle name as a prefix.

Upvotes: 3

Related Questions