【发布时间】:2013-03-30 11:45:47
【问题描述】:
在用户使用 FOSUserBundle 的密码重置来重置他的密码后,默认情况下他会被重定向到 FOSUserProfile。 我想重定向到不同的路线。 这可能吗?如果可以,怎么做?
【问题讨论】:
标签: symfony url-routing fosuserbundle
在用户使用 FOSUserBundle 的密码重置来重置他的密码后,默认情况下他会被重定向到 FOSUserProfile。 我想重定向到不同的路线。 这可能吗?如果可以,怎么做?
【问题讨论】:
标签: symfony url-routing fosuserbundle
可以通过创建一个重置订阅者来完成:
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));
}
}
然后将其注册为带有kernel.event_subscriber标签的服务:
# 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_user_profile_show,比绝对url更好。