动态设计FOS用户捆绑系统的登录页面[英] Dynamically Styling an FOS UserBundle Login Page

本文是小编为大家收集整理的关于动态设计FOS用户捆绑系统的登录页面的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我对Symfony 2是相对较新的,但是我有一个网站,其中有许多不同的子域和用户区域,我希望我的登录页面样式不同,但目前却没有.我使用的是Symfony 2和FOS UserBundle,目前一切正常使用Security.yml中的1个防火墙.根据文档,我是一个覆盖FOS UserBundle布局的覆盖,但我希望能够根据请求的来源来以不同的方式对该页面进行样式. MicroseS1.mainsite.com/user获取样式A MicroseS1.mainsite.com/admin获取样式B Microseing2.Mainsite.Come/用户获取样式C

我考虑了一些选择,我正在寻找其他意见.我考虑过的第一个选项是对FOS用户围栏中的控制器进行覆盖/扩展,以便可以识别转介符并呈现不同的TWIG模板.另一个选择是在不同的路线上使用不同的防火墙,但是我们真的希望能够在所有站点验证的不同微座上的用户,因此首选一个防火墙.还有其他解决方案,或者有一种比另一种解决这个相对较小问题更可取的方法吗?

推荐答案

您可以覆盖 SecurityController的方法.这是您可以做到的:

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

本文地址:https://www.itbaoku.cn/post/2090745.html

问题描述

I am relatively new to Symfony 2 but I have a site with many different sub domains and user areas that I would like my login page styled differently, but currently it is not. I am using Symfony 2 and the FOS UserBundle and everything is currently working properly with 1 firewall in security.yml. I a overriding the FOS UserBundle Layout per the documentation, but I would like to be able to style that page differently depending on where the request is coming from, for example: microsite1.mainsite.com/user gets Style A microsite1.mainsite.com/admin gets Style B microsite2.mainsite.come/user gets Style C

I have considered a few options and I'm looking for other opinions. The first option I had considered was overriding/extending the controllers in the FOS UserBundle so that the referrer could be identified and a different twig template rendered. Another option was to use a different firewall for the different routes, but we really want to be able to have users in different microsites authenticated across all sites so one firewall is preferred. Are there any other solutions to this, or is there one way more preferable than another to tackle this relatively minor issue?

推荐答案

You can override the renderLogin method of the SecurityController. Here is how you could do it:

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