我想禁用默认的Symfony 4.1的行为重定向用户/foo/
到/foo
。禁用此行为的最佳方法是什么?我正在开发 API,所以我不需要这些重定向。
@NicolasB的解决方案太老套了,所以我创建了自定义事件侦听器,用于检查所有带有重定向的响应并将它们转换为 404 错误:
<?php
namespace App\EventListener;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class RedirectionListener
{
public function onKernelResponse(FilterResponseEvent $event): void
{
$response = $event->getResponse();
if ($response->isRedirection()) {
throw new NotFoundHttpException();
}
}
}
不要忘记注册它:
App\EventListener\RedirectionListener:
tags:
- { name: kernel.event_listener, event: kernel.response }
注意:所有重定向都将转换为 404,而不仅仅是尾部斜杠重定向。你应该记住它。但是我正在开发 API,所以我根本不需要任何重定向,所以这个解决方案解决了我的问题。
如果您知道更好的解决方案,欢迎您发表另一个回复!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句