[英]How to call redirectToRoute method accept controller in symfony
I am new in symfony, I do some process after any user login in system, for that make a one service, Code Below : 我是symfony的新手,在任何用户登录系统后我都会执行一些过程,以便提供一项服务,代码如下:
security.authentication.success_handler:
class: AppBundle\Handler\AuthenticationSuccessHandler
arguments: ["@security.http_utils", {}]
tags:
- { name: 'monolog.logger', channel: 'security' }
In Security file Add one line, Code Below : 在安全性文件中添加以下代码行:
success_handler: security.authentication.success_handler
Make a new AuthenticationSuccessHandler
file In folder handler, Code Below : 在文件夹处理程序的下面的代码中新建一个
AuthenticationSuccessHandler
文件:
<?php
namespace AppBundle\Handler;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
use Symfony\Component\Security\Http\HttpUtils;
use Symfony\Component\HttpFoundation\Response;
class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler {
public function __construct(HttpUtils $httpUtils, array $options ) {
parent::__construct( $httpUtils,$options);
}
public function onAuthenticationSuccess( Request $request, TokenInterface $token ) {
$user=$token->getUser();
if (!empty($user) && $user->hasRole('ROLE_MEDICAL_PROVIDER')) {
if($user->getMedicalProvider()->getIsRegistrationCompleted() != 1) {
if($user->getMedicalProvider()->getRequestAssistance() == 1) {
return $this->redirectToRoute('registration_medical_provider_step'.$user->getMedicalProvider()->getRequestAssistanceStep());
} else {
return $this->httpUtils->createRedirectResponse($request, $this->determineTargetUrl($request));
}
} else {
return $this->httpUtils->createRedirectResponse($request, $this->determineTargetUrl($request));
}
}
else {
return $this->httpUtils->createRedirectResponse($request, $this->determineTargetUrl($request));
}
}
}
I want to redirect to another route after login base on condition above code i user and it give me error below: 我想在登录后根据上面的代码我用户的情况重定向到另一条路由,它给我以下错误:
Error: Call to undefined method redirectToRoute() In AuthenticationSuccessHandler.php file
错误:在AuthenticationSuccessHandler.php文件中调用未定义的方法redirectToRoute()
How I call method from login authentication success handler file in symfony ? 如何从symfony中的登录身份验证成功处理程序文件中调用方法?
您不在控制器中,因此可以尝试以下操作:
return $this->httpUtils->createRedirectResponse($request, 'registration_medical_provider_step'.$user->getMedicalProvider()->getRequestAssistanceStep());
You are extending DefaultAuthenticationSuccessHandler, so redirectToRoute() method does not exist in this class. 您正在扩展DefaultAuthenticationSuccessHandler,因此此类中不存在redirectToRoute()方法。
however, you can try this 但是,您可以尝试一下
$this->httpUtils->generateUri($request, 'your_target_path');
Read more HttpUtils and see this method generateUri(Request $request, string $path)
阅读更多HttpUtils并查看此方法
generateUri(Request $request, string $path)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.