Redirect 如何在LoginListener-Symfony2中的onSecurityInteractiveLogin方法中重定向
我们想在登录后重定向到一个或另一个路径。我们还需要在这个路径中传递de语言 我们有一个登录侦听器作为服务来检查这一点。我们的问题是做重定向操作。 代码: 我们的问题是在onSecurityInteractiveLogin函数中重定向到所需的路径 我们试图执行$event->setResponse,但它不起作用。此$event变量没有此方法 有什么想法吗? 谢谢你的帮助Redirect 如何在LoginListener-Symfony2中的onSecurityInteractiveLogin方法中重定向,redirect,symfony,listener,handler,Redirect,Symfony,Listener,Handler,我们想在登录后重定向到一个或另一个路径。我们还需要在这个路径中传递de语言 我们有一个登录侦听器作为服务来检查这一点。我们的问题是做重定向操作。 代码: 我们的问题是在onSecurityInteractiveLogin函数中重定向到所需的路径 我们试图执行$event->setResponse,但它不起作用。此$event变量没有此方法 有什么想法吗? 谢谢你的帮助 编辑:第二个选项(2012年1月27日) 以下: 我试着使用处理器。我配置了services.xml: <service
编辑:第二个选项(2012年1月27日) 以下: 我试着使用处理器。我配置了services.xml:
<service id="company.login_success_handler" class="project\demoBundle\Handler\LoginSucessHandler">
<tag name="monolog.logger" channel="project" />
<argument type="service" id="logger" />
<argument type="service" id="router" />
</service>
在Symfony/src/project/demoBundle/Handler/loginsucesshandler.php中
<?php
namespace project\demoBundle\Handler;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Router;
class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface
{
protected $router;
protected $security;
public function __construct(Router $router, SecurityContext $security)
{
$this->router = $router;
$this->security = $security;
echo "hello world";exit;
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
echo "hello world 2";exit;
if ($this->security->isGranted('ROLE_SUPER_ADMIN'))
{
$response = new RedirectResponse($this->router->generate('category_index'));
}
elseif ($this->security->isGranted('ROLE_ADMIN'))
{
$response = new RedirectResponse($this->router->generate('category_index'));
}
elseif ($this->security->isGranted('ROLE_USER'))
{
// redirect the user to where they were before the login process begun.
$referer_url = $request->headers->get('referer');
$response = new RedirectResponse($referer_url);
}
return $response;
}
}
如果需要更改响应,请使用处理程序而不是事件侦听器。这里有更多信息:我在FOSUserBundle中找到了UrlGeneratorInterface
你好TheOnly92,谢谢你的回答。我试图在代码中使用处理程序,但它不起作用。我想我可能忘了什么。当我登录时,应用程序不会进入OnAuthenticationSuccess。我对services.XML和security.yml进行了配置,示例显示了我是如何再次尝试的,并且效果非常好。@theonly92发送的链接就是我们的答案。谢谢这一联系现在似乎被打破了。
<service id="company.login_success_handler" class="project\demoBundle\Handler\LoginSucessHandler">
<tag name="monolog.logger" channel="project" />
<argument type="service" id="logger" />
<argument type="service" id="router" />
</service>
firewalls:
main:
pattern: /.*
form_login:
check_path: /login_check
login_path: /login
success_handler: company.login_success_handler
use_forward: false
<?php
namespace project\demoBundle\Handler;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Router;
class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface
{
protected $router;
protected $security;
public function __construct(Router $router, SecurityContext $security)
{
$this->router = $router;
$this->security = $security;
echo "hello world";exit;
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
echo "hello world 2";exit;
if ($this->security->isGranted('ROLE_SUPER_ADMIN'))
{
$response = new RedirectResponse($this->router->generate('category_index'));
}
elseif ($this->security->isGranted('ROLE_ADMIN'))
{
$response = new RedirectResponse($this->router->generate('category_index'));
}
elseif ($this->security->isGranted('ROLE_USER'))
{
// redirect the user to where they were before the login process begun.
$referer_url = $request->headers->get('referer');
$response = new RedirectResponse($referer_url);
}
return $response;
}
}
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
...
public function __construct(UrlGeneratorInterface $router)
{
$this->router = $router;
}
...
...
$url = $this->router->generate('homepage');