Symfony2,控制器中的一个动作是否有两条路线? [英] Symfony2, Is it possible to have two route for one action in a controller?

查看:74
本文介绍了Symfony2,控制器中的一个动作是否有两条路线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制器类中有一个动作,我想要两条不同的路线,如下所示:

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.

让我们说:

具有 supervisor角色的用户可以访问 sup_choose_date

具有 user 角色的用户可以访问 user_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 ?

推荐答案

是的,使用YAML(或XML)路由时是可能的。

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

示例:

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

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

这篇关于Symfony2,控制器中的一个动作是否有两条路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆