Drupal菜鸟笔记之订阅和调度事件(路由监听、url监听)

drupal菜鸟笔记之订阅和调度事件(路由监听、url监听)

需求: 某个按钮点击之后需要跳转至一个中转地址,但是中转地址没有页面,所有跳转到中转地址的请求直接定向跳转到指定页面。
参考:
官方文档:
https://www.drupal.org/docs/creating-custom-modules/subscribe-to-and-dispatch-events

实际案例:
1、 services.yml 添加如下代码

ixtend_user.user_registration:
  class: Drupal\ixtend_user\EventSubscriber\RequestAccessSubscriber
  tags:
    - { name: event_subscriber }

第一行: 服务名,我这里是使用改事件的页面服务名,和routing.yml 一致。
第二行:使用事件订阅者的 PHP 类;该类在src/EventSubscriber下面。
第三行:“ tags ”属性,并提供了一个名为“event_subscriber”的标签。这就是服务在系统内注册为事件订阅者的方式。

这里遇到的坑:服务名称使用了一个不存在的服务;因此在使用drush. sh cr命令时报错:
[] operator not supported for strings

2、使用订阅事件的类:
该类必须继承EventSubscriberInterface;并实现 getSubscribedEvents 方法;该方法返回事件名称 => 方法名称键/值对的数组。
checkForRedirection 就是该类中实现自定义逻辑的方法。
第二个值100表示优先级;此值越高,链中的事件侦听器将越早触发

/**
 * {@inheritdoc}
 *
 * @return array
 *   The event names to listen for, and the methods that should be executed.
 */
public static function getSubscribedEvents() {
  $events[KernelEvents::REQUEST][] = ['checkForRedirection', 100];
  return $events;
}

实现自定义逻辑的方法:

/**
 * Request access check.
 *
 * @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
 *   RequestEvent event.
 */
public function checkForRedirection(RequestEvent $event) {
  $current_path = \Drupal::service('path.current')->getPath();

  if ($current_path == '/ixtend') {
    $url = Url::fromRoute('ixtend_user.user_login')->toString();
    $response = new RedirectResponse($url, 301);
    $event->setResponse($response);
  }
}

使用订阅事件的类:

<?php

namespace Drupal\ixtend_user\EventSubscriber;

use Drupal\Core\Url;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
 * Event subscriber subscribing to request.
 */
class RequestAccessSubscriber implements EventSubscriberInterface {

  /**
   * Request access check.
   *
   * @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
   *   RequestEvent event.
   */
  public function checkForRedirection(RequestEvent $event) {
    $current_path = \Drupal::service('path.current')->getPath();

    if ($current_path == '/ixtend') {
      $url = Url::fromRoute('ixtend_user.user_login')->toString();
      $response = new RedirectResponse($url, 301);
      $event->setResponse($response);
    }
  }

  /**
   * {@inheritdoc}
   *
   * @return array
   *   The event names to listen for, and the methods that should be executed.
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = ['checkForRedirection', 100];
    return $events;
  }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值