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

Viewed 4188

I try add global parameter

Parameter for all routes, and parameter setup in kernel Request Listener.

routing

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

in Listener - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest } I try setup this parameter

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

and still have in routes - default value 0

Update 1 i setup listener to max priority

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

in listener setup

 public function onKernelRequest(GetResponseEvent $event)
    {

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

        return ;

and still don't have this parameter is routing.

UPDATE 2

its strange - i dump in Symfony\Component\Routing\Router

in method

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

and get

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

so there exist _applicationid

but in urls i dont have it

here is routing.yml

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

i have links like here: http://crm4.dev/app_dev.php//u/logs/list any ideas ?

UPDATE 3

here is my listeners

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

in CrmApplicationRequestListener :: onKernelRequestInit

i setup onKernelRequestInit and it is visible now.

But still i need change in routing defaults: { _applicationid: 0 }

every path generated by symfony have default _applicationid: 0 not current real _applicationid set in listener. Add current _applicationid for every routing is not possible. I must change default in router. I have hope that problem is clear now.

UPDATE 4 I also try to create custom routing loader - here is question symfony2 add dynamic parameter for routing, try with custom loader

2 Answers
Related