Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 967
Developer Avatar asked Mar 25 '16 16:03

Developer


1 Answers

Your problem isn't very clear, but you might want to check your listener priority is set so that it runs before the router listener.


EDIT:

Assuming you're not trying to set the value of _applicationid in your twig view using path() – e.g. {{ path('path_name', {'_applicationid': 2}) }} – which will work, and may be the better option – you should still be able to set it using a listener.

After examining Symfony's own LocaleListener, I was able to set the value of _applicationid to 2 with the statement $this->router->getContext()->setParameter('_applicationid', 2);. The priority of the subscriber doesn't seem to make any difference, but setting the value in the view takes precedence over the listener.

Framework routing:

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

Bundle Routing:

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

Service definition:

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

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)),
        );
    }
}
like image 187
tomchkk Avatar answered Oct 01 '22 21:10

tomchkk