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

标签 php symfony routing

我尝试添加全局参数

所有路由的参数,以及内核Request Listener中的参数设置。

路由

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

在 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);

并且仍然在路由中 - 默认值 0

更新 1 我将监听器设置为最大优先级

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

在监听器设置中

 public function onKernelRequest(GetResponseEvent $event)
    {

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

        return ;

还没有这个参数就是路由。

更新 2

这很奇怪 - 我转储到 Symfony\Component\Routing\Router

在方法中

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

得到

数组(大小=4) '_controller' => string 'Mea\TaskBundle\Controller\TaskListController::viewOneAction'(长度=59) '_applicationid' => 字符串 '1' (长度=1) 'id' => 字符串 '700' (长度=3) '_route' => 字符串 'MeaTask_View' (length=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

我有这样的链接: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 中

我设置了 onKernelRequestInit,它现在可见。

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

symfony 生成的每条路径都有默认的_applicationid: 0 不是监听器中设置的当前真实_applicationid。 不可能为每个路由添加当前的 _applicationid。我必须更改路由器中的默认值。我希望这个问题现在已经清楚了。

更新 4 我也尝试创建自定义路由加载器 - 这是问题 symfony2 add dynamic parameter for routing, try with custom loader

最佳答案

您的问题不是很清楚,但您可能需要检查您的监听器优先级是否已设置,以便它在路由器监听器之前运行。


编辑:

假设您没有尝试使用 path() 在您的 twig View 中设置 _applicationid 的值——例如{{ path('path_name', {'_applicationid': 2}) }} – 这将起作用,并且可能是更好的选择 – 您仍然应该能够使用监听器设置它。

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

框架路由:

# 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)),
        );
    }
}

关于php - Symfony2 - 如何为每个请求或生成的 URL 的路由添加一个全局参数,类似于 _locale?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36223693/

相关文章:

asp.net-mvc - ASP.NET MVC 搜索路线

api - MapQuest API : Any reason to use it over Google Maps?

php - Symfony:有什么方法可以使用模板的相对路径来渲染 Twig 模板吗?

Symfony2 通过 POST 从 URL 获取数组

ruby-on-rails - 从Devise::SessionsController继承的 Controller 中自定义操作的路由

php - 如何获取我的 jQuery AJAX php 代码来更新 div?

php - 在 symfony 2 中存储自定义实用程序和助手的 Symfonian 方式

php - 在 sentora 网站面板 w.r.t Cpanel [控制面板] 中设置 crons

php - MySQL/PHP 更新查询未执行

php - 使用php将csv文件导入数据库