动态设置FOS UserBundle登录页面的样式

时间:2013-02-09 16:25:06

标签: php symfony fosuserbundle

我对Symfony 2比较陌生,但我有一个网站有很多不同的子域和用户区,我希望我的登录页面的样式不同,但目前不是。我正在使用Symfony 2和FOS UserBundle,目前一切正常,在security.yml中有1个防火墙。我根据文档覆盖了FOS UserBundle布局,但我希望能够根据请求的来源以不同的方式设置该页面的样式,例如:     microsite1.mainsite.com/user获得样式A.     microsite1.mainsite.com/admin获得Style B.     microsite2.mainsite.come / user获得Style C

我考虑了一些选择,我正在寻找其他意见。我考虑的第一个选项是覆盖/扩展FOS UserBundle中的控制器,以便可以识别引用者并呈现不同的树枝模板。另一个选择是为不同的路由使用不同的防火墙,但我们真的希望能够让所有站点中的不同微型网站的用户都经过身份验证,因此首选一个防火墙。对此有任何其他解决方案,还是有一种方法比另一种方法更优选来解决这个相对较小的问题?

1 个答案:

答案 0 :(得分:1)

您可以覆盖SecurityController的{​​{3}}方法。您可以这样做:

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;
    }