首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >组件注入到symfony 3会话

组件注入到symfony 3会话
EN

Stack Overflow用户
提问于 2017-09-27 16:44:00
回答 1查看 202关注 0票数 0

也许有人能帮我

我对symfony不熟悉。运行Symfony 3.3.9和Smarty 3.1.27

我想向会话处理中注入一些东西,这样每次启动会话时都会使用

new Session()

代码语言:javascript
复制
$session = $this->container->get('session');

给出不同的会话值

例如

代码语言:javascript
复制
<?php
namespace AppBundle\Components;

use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;

class MDSession extends Session {

private $domain     =   null;

private $mandant    =   null;

const DEFAULT_THEME = '_default';



    public function __construct(SessionStorageInterface $storage = null,AttributeBagInterface $attributes = null,FlashBagInterface $flashes = null)
    {
        parent::__construct($storage, $attributes, $flashes);

        $this->getDomain();

        /**
         * check if session is set and the same requested domain given
         */
        if(!$this->_get('domain') || $this->_get('domain') != $this->domain)
        {

            $this->mandant = $this->getMandant();

            /**
             * set session here
             */
            $this->_set('domain',   $this->domain);
            $this->_set('mandant',  $this->mandant['id']);
            $this->_set('theme',    $this->mandant['theme']);
        }
    }

    public function _set($name=null,$value=null)
    {
        parent::set($name,$value);
    }

    public function _get($name)
    {
        parent::get($name);
    }


    /**
     * HostnameLookups must be set to On in Apache
     */
    private function getDomain()
    {
        $this->domain = strtolower($_SERVER["HTTP_HOST"]);
    }

    private function getMandant()
    {
         /**
         * do something here
         */
    }
}

如何设置config.yml或services.yml使其正常工作?

EN

回答 1

Stack Overflow用户

发布于 2017-10-08 21:23:51

目前,我使用的EventListener是这样的。

我希望这是正确的方式。

代码语言:javascript
复制
namespace AppBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;


class SessionHandler
{

    const DEFAULT_THEME = '_default';

    const DEFAULT_MANDANT = '1';


    public function onKernelRequest(GetResponseEvent $event)
    {
        $request    = $event->getRequest();
        $session    = $request->getSession();
        $host       = $request->getHost();

        if(!empty($session->get('mandant')) OR $session->get('host') != $host)
        {

            //check DB for mandant

            //.....

            //setting session
            $session->set('host',       $host);
            $session->set('mandant',    self::DEFAULT_MANDANT);
            $session->set('theme',      self::DEFAULT_THEME);
        }


        if (!$event->isMasterRequest()) {
            // don't do anything if it's not the master request
            return;
        }

        // ...
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46443286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档