dongyukui8330
2016-10-18 12:50
浏览 42
已采纳

如何将自定义路由器传递给Symfony Bundle中的服务?

In my Bundle I use a SessionHandler service under FooBundle/Service/SessionHandler.php

This gets passed a customized Router which extends the Symfony Router:

use FooBundle/Routing/Router
    (...)
    public function __construct(HttpUtils $httpUtils, array $options, Router $router) {
        $this->router = $router;
        $this->container = $router->getContainer();
        $this->defaultLogoutHandler = new DefaultLogoutSuccessHandler($httpUtils);
        parent::__construct($httpUtils, $options);
    }

This did work back in Symfony 2.1

With 3.1 I get an Exception:

Catchable Fatal Error: Argument 3 passed to FooBundle\Service\SessionHandler::__construct() must be an instance of FooBundle\Routing\Router, instance of Symfony\Bundle\FrameworkBundle\Routing\Router given

Here is what my services.xml looks like:

<!-- SessionHandler --> <service id="foo.sessionhandler" class="FooBundle\Service\SessionHandler"> <argument type="service" id="security.http_utils" /> <argument type="collection" id="options" /> <argument type="service" id="router" /> </service>

So how do I get this service to use my customized router?

图片转代码服务由CSDN问答提供 功能建议

在我的Bundle中,我使用FooBundle / Service / SessionHandler.php下的SessionHandler服务

这会传递一个扩展Symfony路由器的自定义路由器:

 使用FooBundle / Routing / Router 
(...)
 public function __construct(HttpUtils $  httpUtils,array $ options,Router $ router){
 $ this-&gt; router = $ router; 
 $ this-&gt; container = $ router-&gt; getContainer(); 
 $ this-&gt; defaultLogoutHandler =  new DefaultLogoutSuccessHandler($ httpUtils); 
 parent :: __ construct($ httpUtils,$ options); 
} 
   
 
 

这确实在Symfony 2.1中工作

使用3.1我得到一个异常:

捕获致命错误:参数3传递给FooBundle \ Service \ SessionHandler :: __ construct() 必须是FooBundle \ Routing \ Router的实例,Symfony \ Bundle \ FrameworkBundle \ Routing \ Router的实例

这是我的services.xml的样子:

&lt;! - SessionHandler - &gt; &lt; service id =“foo.sessionhandler”class =“FooBundle \ Service \ SessionHandler”&gt; &lt;参数类型 =“service”id =“security.http_utils”/&gt; &lt; argument type =“collection”id =“options”/&gt; &lt; argument type =“service”id =“router”/&gt; &lt; / service&gt;

那么如何让这项服务使用我的自定义路由器?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douse8732 2016-10-18 14:08
    已采纳

    The router currently being passed is the Symfony Router and hence Symfony Symfony\Bundle\FrameworkBundle\Routing\Router is given.

    You must define your customised Router (FooBundle/Routing/Router) as a service and pass it's service id to the arguments list of service foo.sessionhandler to replace <argument type="service" id="router" />.

    点赞 评论

相关推荐 更多相似问题