如何使用注释路由到同一控制器中的两个不同 'Action' 函数 - Symfony2

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

最近我从 Symfony 2.4 转移到了 Symfony 2.7

所以我一直在关注新文档。现在说我在同一个控制器中有 2 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 的 2 种方法编写注释?

这意味着我在同一个控制器文件中有 indexActionchangeRateAction。我想用 index 函数路由 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