Symfony2:登录后重定向到上一页

时间:2014-06-13 08:55:35

标签: php session symfony authentication login

我想这样做:

用户尝试访问私人页面(需要记录)。当他试图访问时,他无法访问因为他以前必须写他的凭据。他写了他的passowrd并登录系统。我希望用户将自动重定向到他试图访问的页面。

我输入了security.yml

use_referer: true

在我的第一个控制器中,我把:

$this->getRequest()->headers->get('referer');

我总是得到登录的地址(... / login.php)。而不是这个,我需要获得例如(... / downloads / test.txt)

我也尝试过:

$request->getSession()->get('_security.account.target_path');

但我得到了空。

我该怎么做?

提前致谢!

---使用security.yml编辑---

firewalls:
    frontend:
        pattern:     ^/*
        anonymous:   ~
        simple-form:  
            authenticator: my_authenticator
            check_path: login_check
            login_path: login
            use_referer: true
            failure_path: login_error
        logout:
            path:    logout
            target:  /
            success_handler: my_authenticator
        remember_me:
            key:       mykey
            lifetime:  31536000

        switch_user: { role: ROLE_ADMIN, parameter: _login_as }

access_control:
    - { path: ^/web/login.php, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
    - { path: ^/web/login_error.php, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
    - { path: ^/*, roles: [ROLE_USUARIO, ROLE_RESELLER, ROLE_ADMIN], requires_channel: https }

1 个答案:

答案 0 :(得分:0)

Symfony2默认会这样做。如果用户直接进入登录页面(例如通过书签),您只需指定要转到的URL。

请查看如何使用此行为设置“防火墙”:symfony security