【问题标题】:Add function that is called on each request in Symfony2添加在 Symfony2 中对每个请求调用的函数
【发布时间】:2012-10-28 15:52:23
【问题描述】:

我需要在 Symfony2 中添加一个必须在每个请求上调用的函数。 (请求和会话的语言检测)

我想在我的 Controller 类的构造函数中执行此操作,但那里的容器未知/未创建。

您对此有什么建议吗?

【问题讨论】:

    标签: php symfony symfony-2.1


    【解决方案1】:

    你可以定义你的事件监听器

    请阅读有关event listeners 创建的文档。

    【讨论】:

      【解决方案2】:

      这是一个监听器,它重定向到用户配置中设置的语言页面。根据您的需要进行调整。

      <?php
      namespace MyVendor\Listener;
      
      use Symfony\Component\HttpKernel\HttpKernelInterface;
      use Symfony\Component\HttpKernel\Event\GetResponseEvent;
      use Symfony\Component\HttpFoundation\RedirectResponse;
      use Symfony\Component\Security\Core\SecurityContextInterface;
      use Symfony\Component\Routing\RouterInterface;
      use JMS\DiExtraBundle\Annotation\Service;
      use JMS\DiExtraBundle\Annotation\InjectParams;
      use JMS\DiExtraBundle\Annotation\Observe;
      
      /**
       * @Service
       */
      class LanguageListener
      {
          /**
           * @var \Symfony\Component\Security\Core\SecurityContextInterface
           */
          private $securityContext;
      
          /**
           * @var \Symfony\Component\Routing\RouterInterface
           */
          private $router;
      
          /**
           * @InjectParams
           *
           * @param \Symfony\Component\Security\Core\SecurityContextInterface $securityContext
           * @param \Symfony\Component\Routing\RouterInterface $router
           */
          public function __construct(
              SecurityContextInterface $securityContext, 
              RouterInterface $router
          ) {
              $this->securityContext = $securityContext;
              $this->router          = $router;
          }
      
          /**
           * @Observe("kernel.request")
           *
           * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
           */
          public function forceLanguage(GetResponseEvent $event)
          {
              if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
                  return;
              }
      
              $token = $this->securityContext->getToken();
      
              if (!$token) {
                  return;
              }
      
              if (!$this->securityContext->isGranted('ROLE_USER')) {
                  return;
              }
      
              /** @var $request \Symfony\Component\HttpFoundation\Request */
              $request = $event->getRequest();
              $locale  = $request->getLocale();
              $route   = $request->get('_route');
      
              if ('_' === $route[0]) {
                  return;
              }
      
              /** @var $user \MyVendor\Model\User */
              $user = $token->getUser();
      
              if ($user->getConfig()->getLanguage() !== $locale) {
                  $parameters = array_merge($request->attributes->get('_route_params'), [
                      '_locale' => $user->getConfig()->getLanguage(),
                  ]);
      
                  $path = $this->router->generate($route, $parameters);
                  $event->setResponse(new RedirectResponse($path));
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

        我相信这取决于您要做什么。对于语言检测,大部分时间 symfony 及其捆绑软件几乎可以处理所有事情。这意味着如果你想自定义路由,你必须使用routing.loader标签扩展路由组件。 但是,如果您可以使用event listeners,但我不确定您可以从那里更改多少内容。

        【讨论】:

          【解决方案4】:

          按照上面的建议使用事件,或者如果您需要一些快速的东西。

          您可以覆盖setContainer 方法。

          namespace My\Namespace;
          
          use Symfony\Bundle\FrameworkBundle\Controller\Controller;
          use Symfony\Component\DependencyInjection\ContainerInterface;
          
          class MyController extends Controller 
          {
              private $foo;
          
              public function setContainer(ContainerInterface $container = null)
              {
                  parent::setContainer($container);
                  $this->foo = 'bar';
              }
              // your actions
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-03-31
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-01-01
            • 1970-01-01
            • 2017-01-06
            • 1970-01-01
            相关资源
            最近更新 更多