Symfony2 - 如何为每个请求或生成的URL添加一个全局参数来路由类似于_locale?

Dev*_*per 5 php routing symfony

我尝试添加全局参数

所有路由的参数,以及内核请求侦听器中的参数设置.

路由

mea_crm:
    resource: @Crm4Bundle/Resources/config/routing.yml
    prefix: /{_applicationid}
    defaults:  { _applicationid: 0 }
    requirements:
      _applicationid: |0|1|2|3|4|5|6
Run Code Online (Sandbox Code Playgroud)

在Listener中 - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest } 我尝试设置此参数

$request->attributes->add(array(
            '_applicationid'=>$appCurrentId
        ));
        $request->query->add(
            array(
                '_applicationid'=>$appCurrentId
            )
        );
$request->query->set('_applicationid',$appCurrentId);
Run Code Online (Sandbox Code Playgroud)

仍然有路线 - 默认值0

更新1我将侦听器设置为最高优先级

tags:
     - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest, priority: 255 }
Run Code Online (Sandbox Code Playgroud)

在监听器设置中

 public function onKernelRequest(GetResponseEvent $event)
    {

        $event->getRequest()->request->set('_applicationid',1);

        return ;
Run Code Online (Sandbox Code Playgroud)

并且仍然没有这个参数是路由.

更新2

它很奇怪 - 我在Symfony\Component\Routing\Router中转储

在方法中

public function matchRequest(Request $request)
....
var_dump($matcher->matchRequest($request));
        die();
Run Code Online (Sandbox Code Playgroud)

得到

array(size = 4)'_ console'=> string'Mea\TaskBundle\Controller\TaskListController :: viewOneAction'(length = 59)'_ appidid'=> string'1'(length = 1)'id'=> string' 700'(长度= 3)'_ orou'=>字符串'MeaTask_View'(长度= 12)

所以存在_applicationid

但在网址中,我没有

这是routing.yml

mea_crm:
    resource: @MeaCrm4Bundle/Resources/config/routing.yml
    prefix: /{_applicationid}
    defaults:  { _applicationid: null }
    requirements:
      _applicationid: |1|2|3|4|5|6
Run Code Online (Sandbox Code Playgroud)

我有这样的链接:http://crm4.dev/app_dev.php//u/logs/list 任何想法?

更新3

这是我的听众

php app/console debug:event-dispatcher kernel.request

Registered Listeners for "kernel.request" Event
===============================================

 ------- ---------------------------------------------------------------------------------- ---------- 
  Order   Callable                                                                           Priority  
 ------- ---------------------------------------------------------------------------------- ---------- 
  #1      Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure()      2048      
  #2      Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest()     1024      
  #3      Symfony\Component\HttpKernel\EventListener\DumpListener::configure()               1024      
  #4      Mea\Crm4Bundle\Listener\CrmApplicationRequestListener::onKernelRequestInit()       255       
  #5      Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest()    128       
  #6      Symfony\Component\HttpKernel\EventListener\FragmentListener::onKernelRequest()     48        
  #7      Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest()       32        
  #8      Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest()       16        
  #9      FOS\RestBundle\EventListener\BodyListener::onKernelRequest()                       10        
  #10     Symfony\Component\HttpKernel\EventListener\TranslatorListener::onKernelRequest()   10        
  #11     Symfony\Component\Security\Http\Firewall::onKernelRequest()                        8         
  #12     Mea\Crm4Bundle\Listener\CrmApplicationRequestListener::onKernelRequest()           0         
  #13     Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest()      0         
 ------- ---------------------------------------------------------------------------------- ---------- 
Run Code Online (Sandbox Code Playgroud)

在CrmApplicationRequestListener :: onKernelRequestInit中

我设置onKernelRequestInit,它现在可见.

但我仍需要更改路由默认值:{_ applicationid:0}

symfony生成的每个路径都有默认的_applicationid:0不是当前在侦听器中设置的真实_applicationid.无法为每个路由添加当前_applicationid.我必须更改路由器的默认值.我希望现在问题很清楚.

更新4我也尝试创建自定义路由加载器 - 这里是问题 symfony2为路由添加动态参数,尝试使用自定义加载器

tom*_*hkk 5

您的问题不是很清楚,但您可能需要检查侦听器优先级是否设置为使其在路由器侦听器之前运行。

\n\n
\n\n

编辑:

\n\n

假设您不尝试使用\xe2\x80\x93 例如_applicationid在树枝视图中设置 的值path(){{ path(\'path_name\', {\'_applicationid\': 2}) }}设置 的值,这将起作用,并且可能是更好的选项 \xe2\x80\x93 您仍然应该能够使用监听器来设置它。

\n\n

在检查了 Symfony 自己的 后LocaleListener,我能够使用以下语句设置_applicationidto的值2$this->router->getContext()->setParameter(\'_applicationid\', 2);的值。订阅者的优先级似乎没有任何区别,但在视图中设置值优先于侦听器。

\n\n

框架路由:

\n\n
# app/config/routing.yml\nacme_test:\n    resource: "@AcmeTestBundle/Resources/config/routing.yml"\n    prefix:   /{_applicationid}\n    defaults:\n        _applicationid: 0\n    requirements:\n        _applicationid: 0|1|2|3|4\n
Run Code Online (Sandbox Code Playgroud)\n\n

捆绑路由:

\n\n
# src/Acme/TestBundle/Resources/config/routing.yml\nacme_test_home:\n    path:     /\n    defaults: { _controller: AcmeTestBundle:Default:index }\n
Run Code Online (Sandbox Code Playgroud)\n\n

服务定义:

\n\n
# src/Acme/TestBundle/Resources/config/services.yml\nservices:\n    acme_test.listener.app_id_listener:\n        class: Acme\\TestBundle\\EventListener\\AppIdListener\n        arguments:\n            - "@router"\n        tags:\n            - { name: kernel.event_subscriber }  \n
Run Code Online (Sandbox Code Playgroud)\n\n

订户:

\n\n
# src/Acme/TestBundle/EventListener/AppIdListener.php\nnamespace Acme\\TestBundle\\EventListener;\n\nuse Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent;\nuse Symfony\\Component\\HttpKernel\\KernelEvents;\nuse Symfony\\Component\\EventDispatcher\\EventSubscriberInterface;\n\nclass AppIdListener implements EventSubscriberInterface\n{\n    private $router;\n\n    public function __construct($router)\n    {\n        $this->router = $router;\n    }\n\n    public function onKernelRequest(GetResponseEvent $event)\n    {\n        $this->router->getContext()->setParameter(\'_applicationid\', 2);\n    }\n\n    public static function getSubscribedEvents()\n    {\n        return array(\n            KernelEvents::REQUEST => array(array(\'onKernelRequest\', 15)),\n        );\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n