【问题标题】:Getting the route name inside a service in symfony在 symfony 中获取服务中的路由名称
【发布时间】:2016-03-11 12:22:32
【问题描述】:

伙计们,所以我有一个服务,其中我有一个接受路由器服务的构造函数。

我的问题是如何知道当前的路线名称?

这是我的服务:

class Navigation
{
    protected $modules;

    protected $router;

    public function __construct($modules, $router)
    {
        $this->modules = $modules;
        $this->router = $router;
    }
}

谢谢。

【问题讨论】:

    标签: symfony


    【解决方案1】:

    首先你需要在之后注入“@request_stack”

    $requestStack->getCurrentRequest()->get('_route');
    

    【讨论】:

      【解决方案2】:

      或者如果你的类是一个 AbstractController 你可以直接使用它而不需要进一步的依赖注入:

      $this->container->get('request_stack')->getCurrentRequest()->get('_route')
      

      【讨论】:

        猜你喜欢
        • 2019-02-12
        • 1970-01-01
        • 2014-02-16
        • 2018-09-16
        • 1970-01-01
        • 1970-01-01
        • 2017-09-16
        • 1970-01-01
        • 2015-11-26
        相关资源
        最近更新 更多