2013-02-09 113 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; 
    }