2017-09-27 50 views
0

也许有人可以帮我symfony 3的组件注入会话

我对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让它工作?

+0

您的问题并不清楚在所有 – Mcsky

+0

这发生了什么类MDSession以上,应该自动完成,如果会话启动与此 $会议= $ - >容器 - > GET( '会议'); – megadruck

+0

这可能是您尝试将旧习惯应用于新框架的情况。假设您正在使用实际的Symfony框架(而不仅仅是几个组件),那么在会话进入控制器之前,通常会使用内核侦听器来注入这种东西:http://symfony.com/doc/current /event_dispatcher/before_after_filters.html。直接从$ _SERVER提取数据可能不是一个好主意。 – Cerad

回答

0

此刻我用一个像这样的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; 
     } 

     // ... 
    } 
}