我刚刚在这个论坛的帮助下添加了一个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));
}
}
我的security.yml请注意,我在fos_facebook和form_login中都声明了use_referer:
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: ~
当我登录并且我没有完成我的 Profiles 时,我成功地重定向但是当它完成并且referer不为null时我有500个错误:
当我用facebook登录时,我得到一个500错误:"Unable to generate a URL for the named route " http://app.com/app_dev.php/user/login " as such route does not exist."并且firefox中的url是这样的:“http://app.com/app_dev.php/facebook/login_check”
当我使用表单登录时,我收到500错误:"Unable to generate a URL for the named route " http://app.com/app_dev.php/user/login " as such route does not exist."并且firefox中的url是这样的:“http://app.com/app_dev.php/user/login_check”
任何想法,我都疯了 . 我认为它是因为referal存储了用户/登录,当它转到login_check时再次转到用户/登录...
先感谢您!!!
1 回答
您正在尝试使用实际网址生成路线,而不是路线名称 . 试试这段代码: