Symfony2 - How to add a global routing parameter for every request or generated URL like _locale?

I am trying to add a global parameter

Parameter for all routes and parameter settings in the Request Listener kernel.

routing

mea_crm:
    resource: @Crm4Bundle/Resources/config/routing.yml
    prefix: /{_applicationid}
    defaults:  { _applicationid: 0 }
    requirements:
      _applicationid: |0|1|2|3|4|5|6

in the receiver - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest } I am trying to configure this parameter

$request->attributes->add(array(
            '_applicationid'=>$appCurrentId
        ));
        $request->query->add(
            array(
                '_applicationid'=>$appCurrentId
            )
        );
$request->query->set('_applicationid',$appCurrentId);

and still there in routes - the default value is 0

Update 1 i tune the listener to the highest priority

tags:
     - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest, priority: 255 }

in the listener setting

 public function onKernelRequest(GetResponseEvent $event)
    {

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

        return ;

and this parameter has not yet been specified.

UPDATE 2

its weird - I dump in Symfony \ Component \ Routing \ Router

in the method

public function matchRequest(Request $request)
....
var_dump($matcher->matchRequest($request));
        die();

and get

( = 4) '_controller' = > string 'Mea\TaskBundle\Controller\TaskListController:: viewOneAction' (length = 59) '_applicationid' = > string '1' (length = 1) 'id' = > '700' ( = 3) '_route' = > 'MeaTask_View' ( = 12)

_applicationid

URL-

routing.yml

mea_crm:
    resource: @MeaCrm4Bundle/Resources/config/routing.yml
    prefix: /{_applicationid}
    defaults:  { _applicationid: null }
    requirements:
      _applicationid: |1|2|3|4|5|6

, : 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         
 ------- ---------------------------------------------------------------------------------- ---------- 

CrmApplicationRequestListener:: onKernelRequestInit

i onKernelRequestInit .

: {_applicationid: 0}

, symfony, _applicationid: 0 _applicationid, . _applicationid . . , .

4 - symfony2 ,

+4
2

, , .


EDIT:

, _applicationid path() - . {{ path('path_name', {'_applicationid': 2}) }} - - .

Symfony own LocaleListener _applicationid 2 $this->router->getContext()->setParameter('_applicationid', 2);. , , , .

:

# app/config/routing.yml
acme_test:
    resource: "@AcmeTestBundle/Resources/config/routing.yml"
    prefix:   /{_applicationid}
    defaults:
        _applicationid: 0
    requirements:
        _applicationid: 0|1|2|3|4

:

# src/Acme/TestBundle/Resources/config/routing.yml
acme_test_home:
    path:     /
    defaults: { _controller: AcmeTestBundle:Default:index }

:

# src/Acme/TestBundle/Resources/config/services.yml
services:
    acme_test.listener.app_id_listener:
        class: Acme\TestBundle\EventListener\AppIdListener
        arguments:
            - "@router"
        tags:
            - { name: kernel.event_subscriber }  

:

# src/Acme/TestBundle/EventListener/AppIdListener.php
namespace Acme\TestBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class AppIdListener implements EventSubscriberInterface
{
    private $router;

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

    public function onKernelRequest(GetResponseEvent $event)
    {
        $this->router->getContext()->setParameter('_applicationid', 2);
    }

    public static function getSubscribedEvents()
    {
        return array(
            KernelEvents::REQUEST => array(array('onKernelRequest', 15)),
        );
    }
}
+2
0

All Articles