Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony AuthenticationSuccessHandler,登录后重定向并推荐_Symfony_Fosuserbundle - Fatal编程技术网

Symfony AuthenticationSuccessHandler,登录后重定向并推荐

Symfony AuthenticationSuccessHandler,登录后重定向并推荐,symfony,fosuserbundle,Symfony,Fosuserbundle,我刚刚在这个论坛的帮助下添加了一个AuthenticationSuccessHandler,当用户通过fosuserbundle或fosfacebookbundle登录时,它在我的站点上实现了重定向。当用户完成或未完成配置文件时,重定向会发生变化,如果已经完成,我希望将它们重定向到以前的位置,这就是我使用referer变量的原因 namespace Me\MyBundle\Handler; use Symfony\Component\Security\Http\HttpUtil

我刚刚在这个论坛的帮助下添加了一个AuthenticationSuccessHandler,当用户通过fosuserbundle或fosfacebookbundle登录时,它在我的站点上实现了重定向。当用户完成或未完成配置文件时,重定向会发生变化,如果已经完成,我希望将它们重定向到以前的位置,这就是我使用referer变量的原因

namespace Me\MyBundle\Handler; use Symfony\Component\Security\Http\HttpUtils; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler; class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler { protected $router; public function __construct( HttpUtils $httpUtils, array $options, $router) { $this->router = $router; parent::__construct( $httpUtils, $options ); } public function onAuthenticationSuccess( Request $request, TokenInterface $token ) { $user = $token->getUser(); if($user->getProfileComplete()!=1){ //redirects to profile editing $url = 'fos_user_profile_edit'; }else{ $referer = $request->headers->get('referer'); if($referer == NULL){ //redirects to custom url if there is not any referer $url = 'My_customurl'; }else{ //redirects to the referer $url = $referer; } } return new RedirectResponse($this->router->generate($url)); } } 名称空间Me\MyBundle\Handler; 使用Symfony\Component\Security\Http\HttpUtils; 使用Symfony\Component\HttpFoundation\RedirectResponse; 使用Symfony\Component\HttpFoundation\Request; 使用Symfony\Component\Security\Core\Authentication\Token\TokenInterface; 使用Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler; 类AuthenticationSuccessHandler扩展了DefaultAuthenticationSuccessHandler{ 受保护的路由器; 公共函数构造(HttpUtils$HttpUtils,数组$options,$router){ $this->router=$router; 父项::_构造($httpUtils,$options); } 身份验证成功的公共函数(请求$Request,令牌接口$token){ $user=$token->getUser(); 如果($user->getProfileComplete()!=1){ //重定向到配置文件编辑 $url='fos_user_profile_edit'; }否则{ $referer=$request->headers->get('referer'); 如果($referer==NULL){ //如果没有任何引用程序,则重定向到自定义url $url='My_customurl'; }否则{ //重定向到referer $url=$referer; } } 返回新的重定向响应($this->router->generate($url)); } } my security.yml注意,我在fos\u facebook和form\u登录中声明了使用推荐人:

firewalls: main: pattern: ^/ fos_facebook: app_url: "http://apps.facebook.com/app" server_url: "http://www.app.com" login_path: /user/login check_path: /facebook/login_check provider: fos_facebook_provider success_handler: my_auth_success_handler use_referer: true form_login: provider: fos_userbundle csrf_provider: form.csrf_provider login_path: /user/login check_path: /user/login_check use_forward: false success_handler: my_auth_success_handler use_referer: true failure_path: null logout: path: /user/logout anonymous: ~ remember_me: key: mySuperKey lifetime: 4147200 path: / domain: ~ 防火墙: 主要内容: 模式:^/ fos_facebook: 应用程序url:“http://apps.facebook.com/app" 服务器url:“http://www.app.com" 登录路径:/user/login 检查路径:/facebook/登录检查 提供商:fos_facebook_提供商 成功处理程序:我的授权成功处理程序 使用\u referer:true 表格(u)登入: 提供商:fos_用户包 csrf\u提供程序:form.csrf\u提供程序 登录路径:/user/login 检查路径:/user/login\u检查 使用前向:false 成功处理程序:我的授权成功处理程序 使用\u referer:true 失败路径:null 注销: 路径:/user/注销 匿名:~ 记住我: 密钥:mySuperKey 寿命:4147200 路径:/ 域:~ 当我登录并且我没有完成我的配置文件时,我将成功重定向,但是当我登录并且referer不为null时,我有500个错误:

当我使用Facebook登录时,我收到一个500错误:“无法为指定路线生成URL”http://app.com/app_dev.php/user/login“因此,该路由不存在。”firefox中的url是:http://app.com/app_dev.php/facebook/login_check"

当我使用表单登录时,我得到一个500错误:“无法为命名路由生成URL”http://app.com/app_dev.php/user/login“因此,该路由不存在。”firefox中的url是:http://app.com/app_dev.php/user/login_check"


任何想法,我都快疯了。我认为这是因为引用存储了user/login,当它转到login时,检查再次转到user/login。

您尝试使用实际url而不是路由名称生成路由。请尝试以下代码:

if ($user->getProfileComplete() != 1) {
    $url = $this->router->generate('fos_user_profile_edit');
} else {
    $referer = $request->headers->get('referer');
    if ($referer == NULL) {
        $url = $this->router->generate('My_customurl');
    } else {
        $url = $referer;
    }
}

return new RedirectResponse($url);

是的!!!它正在工作,但它将我重定向到登录页面,而不是上一个页面。我想这是因为在referer变量中设置了登录名(这是我最后一次访问的登录名),有没有办法解决这个问题?谢谢:-):-):-)请尝试
$referer=$request->getSession()->get(“\u security.”.$this->providerKey..target\u path',null)
正在变为空,因为正在将我重定向到我的自定义URL。我一直在看这篇文章,但无法使其正常工作:有什么想法吗?任何人