Drupal踩坑笔记之EventSubscriber(事件订阅)

39 篇文章 0 订阅

今天写了一个地址跳转的EventSubscriber;
部分代码:

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

    if ($alias == '/zh-hant/xx') {
      $url = '/zh-hans/xx1';
    }
    elseif ($alias == '/zh-hant/xxa') {
      $url = '/zh-hans/xxb';
    }

    if (isset($url)) {
      $response = new RedirectResponse($url);
      return $response->send();
    }
  }

测试的时候发现第一次访问可以实现跳转,但是第二次就直接not found;但是清除缓存之后就好了。以为是路有缓存的原因。
最后发现跳转的时候应该使用 e v e n t 来 做 , 而 不 是 event来做,而不是 eventresponse.

这样写就没有问题了:

if (isset($url)) {
      $response = new RedirectResponse($url, 302);
      $event->setResponse($response);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The website encountered an unexpected error. Please try again later. AssertionError: Cannot load the "commerce_order_item_type" entity with NULL ID. in assert() (line 295 of core\lib\Drupal\Core\Entity\EntityStorageBase.php). assert(, 'Cannot load the "commerce_order_item_type" entity with NULL ID.') (Line: 295) Drupal\Core\Entity\EntityStorageBase->load(NULL) (Line: 488) Drupal\Core\Entity\EntityBase::load(NULL) (Line: 65) commerce_ticketing_form_commerce_product_variation_type_form_alter(Array, Object, 'commerce_product_variation_type_add_form') (Line: 562) Drupal\Core\Extension\ModuleHandler->alter('form', Array, Object, 'commerce_product_variation_type_add_form') (Line: 840) Drupal\Core\Form\FormBuilder->prepareForm('commerce_product_variation_type_add_form', Array, Object) (Line: 284) Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 73) Drupal\Core\Controller\FormController->getContentResult(Object, Object) call_user_func_array(Array, Array) (Line: 123) Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 580) Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 121) Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97) Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 169) Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 81) Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58) Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48) Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106) Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85) Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48) Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51) Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23) Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 718) Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
最新发布
06-02

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值