也许有人能帮我
我对symfony不熟悉。运行Symfony 3.3.9和Smarty 3.1.27
我想向会话处理中注入一些东西,这样每次启动会话时都会使用
new Session()或
$session = $this->container->get('session');给出不同的会话值
例如
<?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使其正常工作?
发布于 2017-10-08 21:23:51
目前,我使用的EventListener是这样的。
我希望这是正确的方式。
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;
}
// ...
}
}https://stackoverflow.com/questions/46443286
复制相似问题