简体   繁体   English

如何使用注解在同一控制器中路由到两个不同的“动作”功能-Symfony2

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

Recently I shifted from Symfony 2.4 to Symfony 2.7 最近我从Symfony 2.4转移到Symfony 2.7

So I was following the new docs. 所以我一直在关注新文档。 Now say I have 2 action functions in same controller. 现在说我在同一控制器中有2个action functions

public function indexAction() {}

public function changeRateAction()

Previously I would have route them using routing.yml 以前我会使用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 }

Now in 2.7 docs, annotations are encourages. 现在在2.7文档中,鼓励使用注释。 Inside controller file 内部controller文件

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

This will fire the action method contains in the controller file. 这将触发控制器文件中包含的操作方法。 But how can I write annotations for 2 methods for different urls included in the same controller file ? 但是,如何为同一控制器文件中包含的不同URL的2种方法编写注释?

That means I have indexAction & changeRateAction in same controller file. 这意味着我在同一控制器文件中具有indexActionchangeRateAction I want to route url /home with index function and /change with changeRate function. 我想使用索引函数路由url /home ,使用changeRate函数路由/change How to do this using annotations ? 如何使用注释来做到这一点? I know how to do this using routing.yml 我知道如何使用routing.yml

You use the annotation routing on a method, not a controller , really. 您实际上是在方法而不是控制器上使用注释路由。

You just specify the route before every method. 您只需在每种方法之前指定路线。 In your case it would be something like this: 在您的情况下,将是这样的:

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

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

Be sure to read more about routing in the documentation: http://symfony.com/doc/current/book/routing.html 请确保在文档中阅读有关路由的更多信息: http : //symfony.com/doc/current/book/routing.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM