2017-09-27 39 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; 
     } 

     // ... 
    } 
}