我刚刚在这个论坛的帮助下添加了一个 AuthenticationSuccessHandler,它在用户通过 fosuserbundle 或 fosfacebookbundle 登录时在我的网站上实现重定向。当用户完成或未完成配置文件时,重定向会发生变化,如果它完成了,我希望他们被重定向到以前的位置,这就是我使用引用者变量的原因。
命名空间 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 {
受保护的$路由器;
公共函数__construct(HttpUtils $httpUtils,数组$options,$router){
$this->router = $router;
父::__construct($httpUtils, $options);
}
公共函数 onAuthenticationSuccess( 请求 $request, TokenInterface $token ) {
$user = $token->getUser();
if($user->getProfileComplete()!=1){
//重定向到配置文件编辑
$url = 'fos_user_profile_edit';
}别的{
$referer = $request->headers->get('referer');
if($referer == NULL){
//如果没有引用者则重定向到自定义url
$url = 'My_customurl';
}别的{
//重定向到引用者
$url = $推荐人;
}
}
return new RedirectResponse($this->router->generate($url));
}
}
我的 security.yml 注意到我在 fos_facebook 和 form_login 中都声明了 use_referrer:
防火墙:
主要的:
模式:^/
fos_facebook:
app_url:“http://apps.facebook.com/app”
server_url: "http://www.app.com"
登录路径:/用户/登录
check_path:/facebook/login_check
提供者:fos_facebook_provider
成功处理程序:my_auth_success_handler
use_referer: 真
表单登录:
提供者:fos_userbundle
csrf_provider:form.csrf_provider
登录路径:/用户/登录
check_path: /user/login_check
使用转发:假
成功处理程序:my_auth_success_handler
use_referer: 真
失败路径:空
登出:
路径:/用户/注销
匿名:~
记得我:
密钥:mySuperKey
寿命:4147200
小路: /
域名:~
当我登录并且我的个人资料没有完成时,我被成功重定向但是当它完成并且推荐人不为空时,我遇到了 500 个错误:
当我使用 Facebook 登录时,我收到 500 错误:“无法为命名路由“http://app.com/app_dev.php/user/login”生成 URL,因为这样的路由不存在。” Firefox中的网址是:“http://app.com/app_dev.php/facebook/login_check”
当我使用表单登录时,我收到 500 错误:“无法为命名路由“http://app.com/app_dev.php/user/login”生成 URL,因为这样的路由不存在。” Firefox中的网址是:“http://app.com/app_dev.php/user/login_check”
任何想法,我都快疯了。我认为这是因为引荐已经存储了用户/登录名,并且当它转到 login_check 时再次转到用户/登录名。