【问题标题】:Throw 404 page in Observer在观察者中抛出 404 页面
【发布时间】:2015-08-09 12:20:12
【问题描述】:

我有一个观察者:<controller_action_predispatch_catalog_category_view>,它会在我的观察者中触发一个函数来检查用户限制。

如果不允许客户查看此类别,我想向他们显示默认的 Magento 404 错误页面。 这是我目前卡住的地方。

当我查找 404 页面控制器时:<Mage_Cms_IndexController> 它向我展示了这个功能“defaultIndex”:

$this->_forward('defaultIndex');

这是什么功能:

public function defaultIndexAction()
{
    $this->getResponse()->setHeader('HTTP/1.1','404 Not Found');
    $this->getResponse()->setHeader('Status','404 File not found');

    $this->loadLayout();
    $this->renderLayout();
}

但在我的观察者中,我不能调用 loadLayoutrenderLayout 方法,这很明显。

我不想使用 301/302 重定向到 404 页面,我想直接向用户展示它而不进行任何重定向。

我现在得到了什么:

Mage::app()->getFrontController()->getResponse()->setHeader('HTTP/1.1','404 Not Found');
Mage::app()->getFrontController()->getResponse()->setHeader('Status','404 File not found');

- 我需要渲染 404 页面而不是目录布局,但是我该怎么做呢?

任何建议将不胜感激。

【问题讨论】:

  • 你有答案吗?我需要知道你是如何做到这一点的。请发布您的答案

标签: php magento http-status-code-404 magento-1.9 observers


【解决方案1】:

回答:

我无法立即使用->_forward。需要先初始化。

Mage::app()->getFrontController()->getResponse()->setHeader('HTTP/1.1','404 Not Found');
Mage::app()->getFrontController()->getResponse()->setHeader('Status','404 File not found');

$request = Mage::app()->getRequest();
$request->initForward()
    ->setControllerName('indexController')
    ->setModuleName('Mage_Cms')
    ->setActionName('defaultNoRoute')
    ->setDispatched(false);

希望这可以帮助某人:)

【讨论】:

    【解决方案2】:

    我能够在不弄乱请求对象的情况下实现这一点,唷!

    $exception = new Mage_Core_Controller_Varien_Exception("404 THIS REQUEST");
    $exception->prepareForward('norouteAction');
    throw $exception;
    

    成功进入我的商店 404 页面 :)

    原因是它被Mage_Core_Controller_Varien_Action::dispatch 中的catch 拾取,它处理对norouteAction 的转发

    public function dispatch($action)
    {
        try {
            $actionMethodName = $this->getActionMethodName($action);
            if (!method_exists($this, $actionMethodName)) {
                $actionMethodName = 'norouteAction';
            }
    
            Varien_Profiler::start(self::PROFILER_KEY.'::predispatch');
            $this->preDispatch();
            Varien_Profiler::stop(self::PROFILER_KEY.'::predispatch');
    
            if ($this->getRequest()->isDispatched()) {
                /**
                 * preDispatch() didn't change the action, so we can continue
                 */
                if (!$this->getFlag('', self::FLAG_NO_DISPATCH)) {
                    $_profilerKey = self::PROFILER_KEY.'::'.$this->getFullActionName();
    
                    Varien_Profiler::start($_profilerKey);
                    $this->$actionMethodName();
                    Varien_Profiler::stop($_profilerKey);
    
                    Varien_Profiler::start(self::PROFILER_KEY.'::postdispatch');
                    $this->postDispatch();
                    Varien_Profiler::stop(self::PROFILER_KEY.'::postdispatch');
                }
            }
        }
        catch (Mage_Core_Controller_Varien_Exception $e) {
            // set prepared flags
            foreach ($e->getResultFlags() as $flagData) {
                list($action, $flag, $value) = $flagData;
                $this->setFlag($action, $flag, $value);
            }
            // call forward, redirect or an action
            list($method, $parameters) = $e->getResultCallback();
            switch ($method) {
                case Mage_Core_Controller_Varien_Exception::RESULT_REDIRECT:
                    list($path, $arguments) = $parameters;
                    $this->_redirect($path, $arguments);
                    break;
                case Mage_Core_Controller_Varien_Exception::RESULT_FORWARD:
                    list($action, $controller, $module, $params) = $parameters;
                    $this->_forward($action, $controller, $module, $params);
                    break;
                default:
                    $actionMethodName = $this->getActionMethodName($method);
                    $this->getRequest()->setActionName($method);
                    $this->$actionMethodName($method);
                    break;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-24
      • 1970-01-01
      • 1970-01-01
      • 2016-04-20
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多