无法重定向到 magento 2 中的 403 页面

can not redirect to 403 page in magento 2

大家好我正在尝试根据地理位置限制前端的客户 location.Issue 是当我尝试使用 $observer->getControllerAction(); 从观察者发送 403 状态代码时,它 returns 为空。 请查看我使用的代码。 非常感谢任何帮助。

  protected function denyAccess($observer)
    {
       $action = $observer->getControllerAction();
       $response = $action->getResponse();
       $response->clearBody()->setStatusCode(\Magento\Framework\App\Response\Http::STATUS_CODE_403);
    $this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
    }

我已经找到解决上述问题的方法,因为 getControllerAction 方法 returns NULL 由于没有进一步的处理 working.I 不得不使用下面的代码,一切正常。

    protected function denyAccess($observer)
{
    $response = $observer->getResponse();
    $response->clearBody()->setStatusCode(\Magento\Framework\App\Response\Http::STATUS_CODE_403);
    $this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
}