2013-02-09 69 views
1

我對Symfony 2相對較新,但我有一個網站有很多不同的子域和用戶區域,我希望我的登錄頁面樣式不同,但目前不是。我正在使用Symfony 2和FOS UserBundle,並且目前一切正在使用security.yml中的1個防火牆正常工作。我根據文檔重寫了FOS UserBundle佈局,但是我希望能夠根據請求的來源而對頁面進行不同風格的設置,例如: microsite1.mainsite.com/user獲取樣式A microsite1.mainsite .com/admin獲取款式B microsite2.sitesite.come /用戶獲取款式C動態樣式FOS UserBundle登錄頁面

我已經考慮了一些選項,我正在尋找其他意見。我考慮過的第一個選項是覆蓋/擴展FOS UserBundle中的控制器,以便可以識別引用者並呈現不同的樹枝模板。另一種選擇是針對不同路由使用不同的防火牆,但我們確實希望能夠讓所有站點上的不同微型站點中的用戶都通過身份驗證,因此首選防火牆。有沒有其他的解決方案,或者有沒有比另一種更好的方法來解決這個相對較小的問題?

回答

1

您可以覆蓋SecurityControllerrenderLogin方法。下面是你如何做到這一點:

namespace Acme\UserBundle\Controller; 

use FOS\UserBundle\Controller\SecurityController as BaseController; 
use Symfony\Component\DependencyInjection\ContainerAware; 
use Symfony\Component\Security\Core\SecurityContext; 

use Symfony\Component\HttpFoundation\Request; 


class SecurityController extends BaseController 
{ 
    /** 
    * Overriding the FOS default method so that we can choose a template 
    */ 
    protected function renderLogin(array $data) 
    { 
     $template = $this->getTemplate(); 

     return $this->container->get('templating')->renderResponse($template, $data); 
    } 


    /** 
    * You get the subdomain and return the correct template 
    */ 
    public function getTemplate(){ 

     $subdomain = $this->container->get('request')->getHost(); 

     if ($subdomain === "microsite1.mainsite.com"){ 
      $template = sprintf('AcmeUserBundle:Security:loginMicrosite1.html.%s', $this->container->getParameter('fos_user.template.engine')); 
     } 
     elseif($subdomain === "microsite2.mainsite.com"){ 
      $template = sprintf('AcmeUserBundle:Security:loginMicrosite2.html.%s', $this->container->getParameter('fos_user.template.engine')); 
     } 
     //blablabla 
     //Customize with what you need here. 

     return $template; 
    }