【问题标题】:Symfony2 workaround when devices have cookies disabledSymfony2 设备禁用 cookie 时的解决方法
【发布时间】:2014-11-25 20:56:52
【问题描述】:

对于一个项目,我需要为访问者提供一个持久会话。 几年前,我遇到了 Apple 更新临时导致所有 iPhone 无法设置 PHPSESSID cookie 的问题。

我创建了一个备用方法,该方法检查 URL 中的 SESSION ID 并使用它来保持请求之间的会话。我知道这可以在 php.ini 中使用 session.use_trans_sid 启用。

重点是我不希望这种情况总是发生。如果可能,我更喜欢 cookie 方法。 Symfony 中有没有办法将此逻辑添加到添加会话标识符的路由方法中?

谁能帮我解释一下在哪里扩展树枝“路径”方法以添加逻辑以选择性地将会话 ID 附加到由该方法生成的所有 URL。

更新

让我发布我的最新进展,也许有人可以帮助我。我设法通过替换参数中的 generator_base_class 找到了如何用我自己的代码扩展 UrlGenerator。

现在我有以下问题。 我希望使用会话来做一些逻辑。但是,我无法将这个核心组件作为服务。我已经尝试为 UrlGenerator 和扩展的 Router 类创建一个 compilerPass,以便能够在其中一个类中进行依赖注入。

然而直到现在它都不幸失败了。 在 UrlGenerator 类中获取 Session 组件的最佳方法是什么?

【问题讨论】:

    标签: php symfony session cookies


    【解决方案1】:

    感谢这篇文章,我能够创建我的解决方案: Override router and add parameter to specific routes (before path/url used)

    最后这是我想出的代码。

    In my service.xml
    
    <parameters>
        <parameter key="router.class">Acme\CoreBundle\Component\Routing\Router</parameter>
        <parameter key="router.options.generator_base_class">Acme\CoreBundle\Component\Routing\Generator\UrlGenerator</parameter>
    </parameters>
    

    扩展 Symfony 的核心路由器以在 ContainerAware 中创建并将该容器强制到 UrlGenerator。

    namespace Acme\CoreBundle\Component\Routing;
    
    use Symfony\Bundle\FrameworkBundle\Routing\Router as BaseRouter;
    use Symfony\Component\DependencyInjection\ContainerAwareInterface;
    use Symfony\Component\DependencyInjection\ContainerInterface;
    use Symfony\Component\Routing\RequestContext;
    
    class Router extends BaseRouter implements ContainerAwareInterface
    {
        private $container;
    
        public function __construct(ContainerInterface $container, $resource, array $options = array(), RequestContext $context = null)
        {
            parent::__construct($container, $resource, $options, $context);
            $this->setContainer($container);
        }
    
        public function getGenerator()
        {
            $generator = parent::getGenerator();
            $generator->setContainer($this->container);
            return $generator;
        }
    
        public function setContainer(ContainerInterface $container = null)
        {
            $this->container = $container;
        }
    }
    

    扩展 UrlGenerator 类。

    namespace Acme\CoreBundle\Component\Routing\Generator;
    
    use Symfony\Component\Routing\Generator\UrlGenerator as BaseUrlGenerator;
    use Symfony\Component\HttpFoundation\Session\Session;
    use Symfony\Component\DependencyInjection\ContainerAwareInterface;
    use Symfony\Component\DependencyInjection\ContainerInterface;
    
    /**
     * UrlGenerator generates URL based on a set of routes, this class extends the basics from Symfony.
     */
    class UrlGenerator extends BaseUrlGenerator implements ContainerAwareInterface
    {
        private $container;
    
        public function setContainer(ContainerInterface $container = null)
        {
            $this->container = $container;
        }
    
        protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, array $requiredSchemes = array())
        {
            /** @var \Symfony\Component\HttpFoundation\Session\Session $session */
            $session = $this->container->get('session');
            if (true !== $session->get('acceptCookies')) {
                $parameters[$session->getName()] = $session->getId();
            }
    
            return parent::doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, $requiredSchemes);
        }
    }
    

    最终,当会话值 acceptCookies 不等于 true 时,会导致会话名称和 id 被附加到生成的 URL。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-23
      • 1970-01-01
      • 2017-11-18
      • 2013-09-16
      • 2014-07-13
      • 2012-07-19
      • 1970-01-01
      相关资源
      最近更新 更多