Symfony 4。登录前如何修改登录数据?

时间:2019-05-16 19:10:18

标签: symfony symfony4

我想这样更改登录数据:$username = trim(strtolower($username));

为此,我创建了LoginSubscriber。登录前会调用onKernelRequest方法,但是如何在其中修改数据? 也许还有其他方法可以做到?

class LoginSubscriber implements EventSubscriberInterface
{

    /**
     * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
     */
    public function onKernelRequest(GetResponseEvent $event)
    {
        dd($event->getRequest()->request->get('username'));
    }

    public static function getSubscribedEvents()
    {
        return [
            KernelEvents::REQUEST => ['onKernelRequest', 9]
        ];
    }
}

2 个答案:

答案 0 :(得分:0)

尝试更新您的LoginSubscriber

class LoginSubscriber implements EventSubscriberInterface
{

    /**
     * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
     */
    public function onKernelRequest(GetResponseEvent $event)
    {
        $request = $event->getRequest();

        if ($request->request->has('username')) {
             $username = trim(strtolower($request->request->get('username')));
             // Update the username request value
             $request->request->set('username', $username);
        }
    }

    public static function getSubscribedEvents()
    {
        return [
            KernelEvents::REQUEST => ['onKernelRequest', 9]
        ];
    }
}

答案 1 :(得分:0)

您是否尝试在services.yml配置文件中为列出的请求侦听器设置更高的优先级priority: 200?尝试执行bin /控制台debug:event-dispatcher命令以查看执行队列