扩展layout.html.twig后,FOSUserBundle登录表单未显示

时间:2014-04-15 14:34:51

标签: symfony fosuserbundle

我已阅读该指南,在安装,配置和覆盖默认模板后,我可以浏览到我的登录页面,但只显示我的页眉和页脚,没有登录表单。

config.yml:

fos_user:
    db_driver: orm
    firewall_name: main
    user_class: WX\ExchangeBundle\Entity\TblUser

的routing.yml:

fos_user_security:
    resource: "@FOSUserBundle/Resources/config/routing/security.xml"

fos_user_profile:
    resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
    prefix: /Dashboard

fos_user_register:
    resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
    prefix: /SignUp

fos_user_resetting:
    resource: "@FOSUserBundle/Resources/config/routing/resetting.xml"
    prefix: /ResetPassword

fos_user_change_password:
    resource: "@FOSUserBundle/Resources/config/routing/change_password.xml"
    prefix: /profile

security.yml:

# app/config/security.yml
security:
    encoders:
        FOS\UserBundle\Model\UserInterface:
            algorithm: bcrypt
            cost: 15

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username_email

    firewalls:
        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_provider: form.csrf_provider
                login_path: /Login
            logout:       true
            anonymous:    true

    access_control:
        - { path: ^/Login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/SignUp, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/ResetPassword, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/Confirm, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/Dashboard/, role: ROLE_ADMIN }

应用\资源\ FOSUserBundle \视图\ layout.html.twig:

{% extends '::base.html.twig' %}

{% block title %}{% endblock %}

{% block body %}
    {% block fos_user_content %}{% endblock fos_user_content %}
{% endblock %}

控制器:

namespace WX\ExchangeBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use JMS\SecurityExtraBundle\Annotation\Secure;

class UserController extends Controller
{
    /**
     * @Route("/Login", name="wx_exchange_login")
     * @Template("WXExchangeBundle:User:login.html.twig")
     * User log up - Open to public
     * Authenticates users to the system
     */
    public function loginAction()
    {
        return array();
    }

    /**
     * @Route("/Dashboard", name="wx_exchange_dashboard")
     * @Template("WXExchangeBundle:User:user.html.twig")
     * @Secure(roles="IS_AUTHENTICATED_FULLY")
     * Dashboard - secured for authenticated users
     * Determines if a user is logged in and redirects them to the user dashboard or login page
     */
    public function dashboardAction()
    {
        return array();
    }
}

WXExchangeBundle:用户:login.html.twig:

{% extends 'FOSUserBundle::layout.html.twig' %}

{% block title %}Login{% endblock %}

{% block body %}
    {% block fos_user_content %}{% endblock fos_user_content %}
{% endblock %}

从文档中我不清楚我是否应该在Controller操作中返回一些内容。这是问题吗?

2 个答案:

答案 0 :(得分:1)

我通过在我自己的登录文件中扩展login.html.twig文件来实现此目的:

{% extends 'FOSUserBundle:Security:login.html.twig' %}

答案 1 :(得分:0)

你只需要块

{% block fos_user_content %}{% endblock fos_user_content %}
你的layout.html.yml中的

,而不是你的base.html.yml。

此外,您需要使用返回来呈现模板。

/**
 * @Route("/Login", name="wx_exchange_login")
 * @Template("WXExchangeBundle:User:login.html.twig")
 * User log up - Open to public
 * Authenticates users to the system
 */
public function loginAction()
{
    return $this->render('WXExchangeBundle:User:login.html.twig');
}

而不是返回数组