简体   繁体   English

Symfony:登录后不会重定向回请求的页面

[英]Symfony: After login doesn't redirect back to requested page

I want to redirect back to requested page after login, I know symfony provides that by default, but it's not working in my case. 我想在登录后重定向回请求的页面,我知道symfony默认提供该功能,但在我的情况下不起作用。 I have tried with referer: true in security.yml but didn't get any result. 我尝试使用referer: truesecurity.yml中为 referer: true ,但未得到任何结果。

I am using FOSUserBundle for User management. 我正在使用FOSUserBundle进行用户管理。 Your help will be appreciated, thanks in advance. 在此先感谢您的帮助。

security.yml 安全性

security:
 encoders:
    FOS\UserBundle\Model\UserInterface: bcrypt
    Admin\UserBundle\Entity\SuperAdmin: bcrypt
  role_hierarchy:
    ROLE_DEFAULT:     ROLE_DEFAULT
    ROLE_SUPER_ADMIN: ROLE_SUPER_ADMIN
  providers:
    in_memory:
        memory: ~
    fos_userbundle:
      id: fos_user.user_provider.username_email
    administrators:
        entity: { class: Admin\UserBundle\Entity\SuperAdmin, property: email}
  firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    social:
        pattern: ^/            
        form_login:
            provider: fos_userbundle
            csrf_token_generator: security.csrf.token_manager
            login_path: fos_user_security_login
            check_path: fos_user_security_check
            default_target_path: user_bundle_homepage
            success_handler: my.authentication_success_handler
        logout:
            path:   fos_user_security_logout
            target: fos_user_security_login
        anonymous: true

  access_control:
    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https  }        
    - { path: ^/login/facebook$, role: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
    - { path: ^/login/google$, role: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https  }
    - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
    - { path: ^/admin0rama/.*, role: ROLE_SUPER_ADMIN, requires_channel: https}
    - { path: ^/.*, role: ROLE_DEFAULT, requires_channel: https}

I guess you have to use use_referer: true in your firewall definition (not just referer ). 我猜您必须在防火墙定义中使用use_referer: true (而不仅仅是referer )。

Example: 例:

# security.yaml
security:
    firewalls:
        social:
            form_login:
                use_referer: true

The documentation . 文档

Can you try with the _target_path, you can add this field to your login form, this works always for me in my projects 您可以尝试使用_target_path吗,可以将此字段添加到登录表单中吗,这在我的项目中始终对我有效

{% if app.request.get('_route') != app.request.headers.get('referer') %}    
    <input type="hidden" name="_target_path" value="{{ app.request.headers.get('referer') }}" />    
{% endif %}

I hope this can help you 希望对您有所帮助

How to Customize Redirect After Form Login 表单登录后如何自定义重定向

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM