![](/img/trans.png)
[英]Symfony2 redirect /admin/* to /admin/login when not logged in (or use FOSUserBundle?)
[英]Symfony2 / FOSUserBundle - Redirect after login according to the role
我想根据用户的角色在登录后自定义重定向。
仅供参考:我使用的是symfony 2.8
我创建这个类:
<?php
namespace Users\UsersBundle\Redirection;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
class AfterLoginRedirection implements AuthenticationSuccessHandlerInterface
{
protected $router;
protected $security;
/**
* AfterLoginRedirection constructor.
* @param Router $router
* @param Security $security
*/
public function __construct(Router $router, Security $security)
{
$this->router = $router;
$this->security = $security;
}
/**
* @param Request $request
* @param TokenInterface $token
* @return RedirectResponse
*/
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
$response = new RedirectResponse($this->router->generate('_homepage_admin'));
} else {
$referer_url = $request->headers->get('referer');
$response = new RedirectResponse($referer_url);
}
return $response;
}
}
我创建这个服务:
services:
redirect.after.login:
class: Users\UsersBundle\Redirection\AfterLoginRedirection
arguments: [@router]
我修改了防火墙
firewalls:
main:
pattern: ^/
form_login:
login_path: fos_user_security_login
check_path: fos_user_security_check
provider: fos_userbundle
csrf_provider: form.csrf_provider
success_handler: redirect.after.login
logout:
path: /users/logout
target: /
anonymous: true
我收到了这个错误:
可捕获的致命错误:传递给Users \\ UsersBundle \\ Redirection \\ AfterLoginRedirection :: __ construct()的参数1必须是Users \\ UsersBundle \\ Redirection \\ Router的实例,Symfony \\ Bundle \\ FrameworkBundle \\ Routing \\ Router的实例,在C中调用:第2060行的\\ wamp \\ www \\ eCommerce \\ app \\ cache \\ dev \\ appDevDebugProjectContainer.php并定义
我错过了什么? 任何线索或建议?
谢谢。
<?php
namespace Users\UsersBundle\Redirection;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Router;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
class AfterLoginRedirection implements AuthenticationSuccessHandlerInterface
{
protected $router;
protected $security;
/**
* AfterLoginRedirection constructor.
* @param Router $router
* @param AuthorizationChecker $security
*/
public function __construct(Router $router, AuthorizationChecker $security)
{
$this->router = $router;
$this->security = $security;
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
$response = new RedirectResponse($this->router->generate('_homepage_admin'));
} else {
$referer_url = $request->headers->get('referer');
$response = new RedirectResponse($referer_url);
}
return $response;
}
}
服务定义:
redirect.after.login:
class: Users\UsersBundle\Redirection\AfterLoginRedirection
arguments: ['@router','@security.authorization_checker']
我所做的改变:
Router
而不是RouterInterface
@security.authorization_checker
注入redirect.after.login
服务
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.