我的订户如下:
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpFoundation\RedirectResponse;
class KernelSubscriber implements EventSubscriberInterface {
public function __construct() {
// some stuff here but not relevant for this example
}
public static function getSubscribedEvents(): array {
return [ KernelEvents::REQUEST => 'onRequest' ];
}
public function onRequest(GetResponseEvent $event): void {
// if conditions met
// 301 redirect to some internal or external URL
if(!$event->isMasterRequest()) return;
}
}
如果这是一个控制器,我会返回
$this->redirectToRoute('route')
或者类似的,但是从
onRequest
如何从该事件订阅服务器返回响应(特别是重定向)?