Symfony2, Is it possible to have two route for one action in a controller?

二次信任 提交于 2019-12-21 17:14:48

问题


I have an action inside my controller class and I want two different routes like below:

/**
 * Displays a form to create a new entity.
 *
 * @Route("/edit/choose/date", name="user_choose_date")
 * @Route("/supervisory/choose/date", name="sup_choose_date")
 * @Template()
 */
public function chooseDateAction()
{
    return array( );
}

The reason for that I would like to give the route access to some users but the user role are different.

Let's say:

User with supervisor role can access sup_choose_date

User with user role can access user_choose_date

The question is if it is possible to have two different routes for one action? or I have duplicate the code for different routes ?


回答1:


Yes, it is possible when using YAML (or XML) routing.

Example:

sup_choose_date:
    pattern:   /supervisory/choose/date
    defaults:  { _controller: MyBundle:Default:chooseDate }

user_choose_date:
    pattern:   /edit/choose/date
    defaults:  { _controller: MyBundle:Default:chooseDate }



回答2:


I is possible on every kind of format including annotation. It should work as long as you have different name for every route.




回答3:


Worked for me!

You must set different names; if not, specify explicitly



来源:https://stackoverflow.com/questions/11127414/symfony2-is-it-possible-to-have-two-route-for-one-action-in-a-controller

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