Convention-based routing in Symfony2

北城以北 提交于 2019-12-30 11:15:11

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/16677330/convention-based-routing-in-symfony2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!