2

I'm trying to learn learn how routing works in Symfony2, and so far everything I've read has examples like this:

blog:
    path:      /blog/{page}
    defaults:  { _controller: AcmeBlogBundle:Blog:index, page: 1 }

This routes requests to /blog/123 to the AcmeBlogBundle Blog controller's "index" action, and passes the 123 parameter as the "page" parameter to that controller action. If no page parameter is passed, then the page defaults to 1.

That's all well and good, but what if you want to simply have a convention based routing system that passes things through like this:

/{bundle}/{controller}/{action}

So, for a URL like this:

/acme/blog/index

It would then call AcmeBlogBundle Blog controller's "index" action.

No specific routing configuration is necessary, it simply infers the bundle, controller, and action from the URL. So you can continue adding bundles, controllers, and actions, and you don't need to modify the routing configuration. It just works.

If this isn't possible, can you at least infer the controller and action from the URL? E.g., perhaps you need a route that specifically identifies the bundle, but can we get the controller and action from the URL?

I read through the Symfony "The Book" page about routing, and I couldn't figure out a way to do this.

Josh
  • 7,232
  • 8
  • 48
  • 75

1 Answers1

0

No way. This was considered as bad practice and so it was removed from symfony.

But you should take a look at the @Route annotation, as it simplifies configuring routes in such a nice way. Directly attached to the action, there is no lack between config and code.

Emii Khaos
  • 9,983
  • 3
  • 34
  • 57
  • 2
    Why is this considered bad practice? – Josh May 21 '13 at 19:31
  • the best practice (for symfony1) said to disable it and use custom routes – Emii Khaos May 21 '13 at 19:57
  • 1
    But why is it considered bad practice? What does the extra configuration of routes that correspond to each controller and action give you that having automatic, convention-based routes doesn't give you? – Josh May 21 '13 at 20:40
  • with the old default routing you exposed the internals of your application. You have only id's in the url, not good. symfony routing is composed to create meaningful and readable urls. – Emii Khaos May 21 '13 at 20:57
  • 4
    But in cases like mine the "meaningful urls" correspond 1:1 to the "internals of my app". Therefore I am still "exposing" the same data, I'm just being forced to repeat myself by defining my bundles, controllers, and actions and then creating routes that could have been inferred from those bundles controllers and routes.. Nonetheless, I accept that this is "just the way it is" in Symfony2, and so your answer is right and the workaround to repeat yourself in the @Route directive seems the least tedious. Thanks! – Josh May 21 '13 at 21:05