1

我浏览了http://tutorial.symblog.co.uk/index.html上的 symfony 博客教程,并希望将其扩展为需要身份验证。我松散地遵循http://symfony.com/doc/current/cookbook/security/entity_provider.html,创建了一个新的 Bundle(我想稍后将它提取到一个公共区域)并且一切正常。唯一的问题是在登录页面上,它没有显示 symfony 工具栏(它在其他任何地方都显示),但页面的其余部分按我的预期显示。

有任何想法吗?提前致谢。

我的 login.html.twig:

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

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

{% block body %}
    {% if error %}
        <div class="error-message">{{ error.message }}</div>
    {% endif %}

    <form action="{{ path('login_check') }}" method="post">
        <label for="username">E-mail address:</label>
        <input type="text" id="username" name="_username" value="{{ last_username }}"/>

        <label for="password">Password:</label>
        <input type="password" id="password" name="_password"/>

        {#
            If you want to control the URL the user
            is redirected to on success (more details below)
            <input type="hidden" name="_target_path" value="/account" />
        #}

        <button type="submit">login</button>
    </form>

{% endblock %}

我的 ::base.html.twig:

<!-- app/Resources/views/base.html.twig -->
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html"
    ; charset=utf-8" />
    <title>{% block title %}symblog{% endblock %} - symblog</title>
    <!--[if lt IE 9]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    {% block stylesheets %}
        <link href='http://fonts.googleapis.com/css?family=Irish+Grover' rel='stylesheet' type='text/css'>
        <link href='http://fonts.googleapis.com/css?family=La+Belle+Aurore' rel='stylesheet' type='text/css'>
        <link href="{{ asset('css/screen.css') }}" type="text/css" rel="stylesheet"/>
    {% endblock %}
    <link rel="shortcut icon" href="{{ asset('favicon.ico') }}"/>
</head>
<body>

<section id="wrapper">
    <header id="header">
        <div class="top">
            {% block navigation %}
                <nav>
                    <ul class="navigation">
                        <li><a href="{{ path('BloggerBlogBundle_homepage') }}">Home</a></li>
                        <li><a href="{{ path('BloggerBlogBundle_about') }}">About</a></li>
                        <li><a href="{{ path('BloggerBlogBundle_contact') }}">Contact</a></li>
                        {% if app.user %}
                            <li><a href="{{ path('logout') }}">Logout {{ app.user.username }}</a></li>
                        {% endif %}
                    </ul>
                </nav>
            {% endblock %}
        </div>

        <h2>{% block blog_title %}<a href="{{ path('BloggerBlogBundle_homepage') }}">symblog</a>{% endblock %}</h2>

        <h3>{% block blog_tagline %}<a href="{{ path('BloggerBlogBundle_homepage') }}">creating a blog in
                Symfony2</a>{% endblock %}</h3>
    </header>

    <section class="main-col">
        {% block body %}{% endblock %}
    </section>
    <aside class="sidebar">
        {% block sidebar %}{% endblock %}
    </aside>

    <div id="footer">
        {% block footer %}
            Symfony2 blog tutorial - created by <a href="https://github.com/dsyph3r">dsyph3r</a>
        {% endblock %}
    </div>
</section>

{% block javascripts %}{% endblock %}
</body>
</html>
4

1 回答 1

1

编辑:我找到了答案……在 access_control 下查看。

没有任何东西被切断。我听说这是因为工具栏在访问权限下没有正确设置,但我不完全确定如何做到这一点。这是我的security.yml:

security:
    encoders:
        Database\UserBundle\Entity\User:
            algorithm:          sha1
            encode_as_base64:   false
            iterations:         1

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

    providers:
        administrators:
            entity: { class: DatabaseUserBundle:User, property: email }

    firewalls:
        login_firewall:
            pattern:    ^/login$
            anonymous:  ~
        secured_area:
            pattern:    ^/
            anonymous:  ~
            form_login:
                login_path:                     login
                check_path:                     login_check
                always_use_default_target_path: true
                default_target_path:            /
            logout:
                path:   /logout
                target: /

    access_control:
        - { path: /_wdt/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }  ### These 2 lines needed
        - { path: /_profiler/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }### These 2 lines needed
        - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/, roles: ROLE_USER }
于 2013-09-04T15:20:52.473 回答