如何路由到两个不同的';操作';使用注释Symfony2在同一控制器中执行函数


How to route to two different 'Action' functions in same controller using annotation - Symfony2

最近我从Symfony 2.4转到Symfony 2.7

所以我一直在关注新的文档。现在假设我在同一个控制器中有两个action functions

public function indexAction() {}
public function changeRateAction()

以前我会使用routing.yml 对它们进行路由

change_interest_rate_label:
    path: /change_interest_rate
    defaults: { _controller: appBundle:appInterestRateChange:index }
change_interest_rate_action_label:
    path: /change_interest_rate_and_archived_action
    defaults: { _controller: appBundle:appInterestRateChange:changeRate }

现在在2.7文档中,鼓励使用注释。内部controller文件

/**
 * @Route("/home", name="homepage")
 */

这将激发控制器文件中包含的操作方法。但是,我如何为同一控制器文件中包含的两个不同url的方法编写注释呢?

这意味着我有indexAction&changeRateAction在同一控制器文件中。我想用索引函数路由url /home,用changeRate函数路由/change。如何使用注释做到这一点?我知道如何使用routing.yml

实际上,在方法上使用注释路由,而不是控制器

您只需在每个方法之前指定路线。在你的情况下,它会是这样的:

/**
 * @Route("/home", name="homepage")
 */
public function indexAction() {
    ...
}
/**
 * @Route("/change", name="changerate")
 */
public function changeRateAction() {
    ...
}

请务必阅读文档中有关路由的更多信息:http://symfony.com/doc/current/book/routing.html