I am new in symfony, I do some process after any user login in system, for that make a one service, Code Below :
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 :
<?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
How I call method from login authentication success handler file in 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.
however, you can try this
$this->httpUtils->generateUri($request, 'your_target_path');
Read more HttpUtils and see this method generateUri(Request $request, string $path)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.