关于如何构建传统登录表单的Symfony 3错误

时间:2016-02-09 10:13:52

标签: php symfony

尝试在this页上使用该教程

问题在于我总是回到登录表单,并且无法完成我所缺少的内容。 我使用的是Synfony 3版本。 谢谢你的帮助:)

security:
    encoders:
        Symfony\Component\Security\Core\User\User: plaintext
    providers:
        in_memory:
            memory: 
                users:
                    ryan:
                        password: ryanpass
                        roles: 'ROLE_USER'
                    admin:
                        password: admin
                        roles: 'ROLE_USER'
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false


        home:
            pattern: ^/home$
            form_login:
                login_path: login
                check_path: login
                always_use_default_target_path: true

        main:
            anonymous: ~


=========== routing
login:
    path:   /login
    defaults:  { _controller: AppBundle:Security:login }

btw_user:
    resource: "@BTWUserBundle/Resources/config/routing.yml"
    prefix:   /

btw_menus:
    resource: "@BTWMenusBundle/Resources/config/routing.yml"
    prefix:   /

app:
    resource: "@AppBundle/Controller/"
    type:     annotation

btw_home:
    resource: "@BTWHomeBundle/Resources/config/routing.yml"
    prefix:   /


============ controller
namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;


class SecurityController extends Controller {

    public function loginAction(Request $request) {
        //var_dump($request);

        $authenticationUtils = $this->get("security.authentication_utils");
        //var_dump($_POST);
        // get the login error if there is one
        $error = $authenticationUtils->getLastAuthenticationError ();
        var_dump($error);
        // last username entered by the user
        $lastUsername = $authenticationUtils->getLastUsername();
        var_dump($lastUsername);
        return $this->render ( 'security/login.html.twig',
                array(
            // last username entered by the user
            'last_username' => $lastUsername,
            'error'         => $error,
        ));
    }
}

1 个答案:

答案 0 :(得分:0)

我也有同样的问题,这解决了我的问题:

上下文:session_key Read more

在admin_login yaml block创建额外的form_login。

这不是解决此问题的最佳方法,我会进行测试以避免这种情况,但它确实有效!

用法:

admin_login:
  pattern:  ^/admin/login$
  anonymous: ~
  provider: user_provider
  context: session_key
  form_login:
    login_path: admin_login
    check_path: admin_login
    default_target_path: admin_category_index
admin:
  pattern: ^/admin
  provider: user_provider
  context: session_key
  form_login:
    login_path: admin_login
    check_path: admin_login
    default_target_path: admin_category_index
  logout:
    path:   /logout
    target: /
    invalidate_session: false