这可以通过利用内置交响曲EventDispatcher Component来实现。您要做的就是创建一个自定义模块。添加您的services.yml文件并提供适当的服务配置。
services:
mymodue.subscriber:
class: Drupal\my_module\EventSubscriber
tags:
- { name: event_subscriber }
在您的模块src目录中,添加创建您的EventSubscriber.php类并在此处描述您的方法。
<?php
use Symfony\Component\HttpFoundation\RedirectResponse;
public function checkForCustomRedirect(GetResponseEvent $event) {
$route_name = \Drupal::request()->attributes->get(RouteObjectInterface::ROUTE_NAME);
if($route_name === 'module.testPage') {
$event->setResponse(new RedirectResponse($url, $status = 302,$headers);
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events = [];
$events[KernelEvents::REQUEST][] = array('checkForCustomRedirect');
return $events;
}