ログイン成功後のカスタム ロールベース リダイレクト用のカスタム成功ログイン ハンドラがあります。
配列内のこれらの特別なロールのいずれ$specialRoles
かが見つかると、新しいRedirectResponse
が返されます。
/** @DI\Service("handler.login_sucess_handler") */
class LoginSuccessHandler implements uthenticationSuccessHandlerInterface
{
private $router;
private $securityContext;
/**
* @DI\InjectParams({
* "securityContext"= @DI\Inject("security.context"),
* "router" = @DI\Inject("router") })
*/
public function __construct(SecurityContext $securityContext,
I18nRouter $router)
{
$this->securityContext = $securityContext;
$this->router = $router;
}
public function onAuthenticationSuccess(Request $request,
TokenInterface $token)
{
$specialRoles = array('ROLE_1', 'ROLE_2', 'ROLE_3');
foreach($specialRoles as $specialRole)
{
if($this->securityContext->isGranted($specialRole))
{
$redirectUrl = $this->router->generate('manage_index');
return new RedirectResponse($redirectUrl);
}
}
}
}
一方、ユーザーに特別なロールがない場合にどうなるかを指定する必要があります。/app/dashboard
つまり、デフォルトルートをハードコーディングしないでください。つまり、ファイルdefault_target_path
から (ある場合) を読み取ります。security.yml
form_login:
login_path: /app/login
check_path: /app/login_check
default_target_path: /app/dashboard
success_handler: handler.login_sucess_handler
これを行う方法はありますか?
もちろん、コードをそのままにしておくと、ユーザーが特別な役割を持っていない場合、例外がスローされます。
キャッチ可能な致命的なエラー: Symfony\Component\HttpKernel\Event\GetResponseEvent::setResponse() に渡される引数 1 は、Symfony\Component\HttpFoundation\Response のインスタンスでなければなりません。null を指定してください。