【问题标题】:How to route to two different 'Action' functions in same controller using annotation - Symfony2如何使用注释在同一控制器中路由到两个不同的“动作”功能 - Symfony2
【发布时间】:2015-11-11 11:52:52
【问题描述】:

最近我从 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

【问题讨论】:

    标签: php symfony symfony-2.7


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 啊!错过了。完美的。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2020-02-27
    • 2019-10-26
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多