Symfony FOSUserBundle:密码重置后的成功目标
在用户使用FOSUserBundle的密码重置功能重置其密码后,默认情况下,用户会重定向到FOSUserProfile。 我想重定向到另一条路线。Symfony FOSUserBundle:密码重置后的成功目标,symfony,url-routing,fosuserbundle,Symfony,Url Routing,Fosuserbundle,在用户使用FOSUserBundle的密码重置功能重置其密码后,默认情况下,用户会重定向到FOSUserProfile。 我想重定向到另一条路线。 这是否可能?如果可能,如何实现?可以通过创建重置订户来实现: namespace Acme\UserBundle\EventListener; use FOS\UserBundle\FOSUserEvents; use FOS\UserBundle\Event\FormEvent; use Symfony\Component\EventDispat
这是否可能?如果可能,如何实现?可以通过创建重置订户来实现:
namespace Acme\UserBundle\EventListener;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* Listener responsible to change the redirection at the end of the password resetting
*/
class PasswordResettingListener implements EventSubscriberInterface {
private $router;
public function __construct(UrlGeneratorInterface $router) {
$this->router = $router;
}
public static function getSubscribedEvents() {
return [
FOSUserEvents::RESETTING_RESET_SUCCESS => 'onPasswordResettingSuccess',
];
}
public function onPasswordResettingSuccess(FormEvent $event) {
$url = $this->router->generate('homepage');
$event->setResponse(new RedirectResponse($url));
}
}
然后将其注册为具有内核的服务。事件\u订阅者
标记:
# src/Acme/UserBundle/Resources/config/services.yml
services:
acme_user.password_resetting:
class: Acme\UserBundle\EventListener\PasswordResettingListener
arguments: [ @router ]
tags:
- { name: kernel.event_subscriber }
如果您不使用FOS用户配置文件视图,有一种丑陋但简单的方法: 添加你的
app/config/routing.yml
:
fos_user_profile_show:
path: /yourpath
注意:此解决方案要求您使用FOS Userbundle的主版本。您可以通过扩展重置控制器并更改getRedirectionUrl()方法来获得类似的结果。注意:由于Symfony 4,您不需要在service.yml中指定路由器参数。您还可以在自己的控制器中使用名称
fos\u user\u profile\u show
,声明路由,优于绝对url。@Loutrailloune在上述路由配置中没有绝对url。看起来可能是这样,但事实并非如此