2014-04-04 54 views
2

我有一个自定义路由的应用程序 - 万一有一个不可用的URL被调用,会引发异常。在catch块,我试图如何在引导程序中在Zend框架内重定向404

  • 发送404错误
  • 显示找不到网页(通过/ NOTFOUND可用)的自定义

我怎样才能做到这一点?如果我重定向到该页面,它始终会执行302重定向... - 然后我的想法是从引导内呈现未找到的视图

谢谢!

+1

你不能“重定向”到404页面。根据定义,重定向(HTTP状态302或301)不是404。另外,ZF应该默认执行你的操作,如果这是ZF1,请查看错误控制器。 –

回答

0

我做的方式:

1-在public,我添加一个文件redirect404.html约4040错误的特定消息。

2-在errorAction()ErrorController我做这样的事情:

$errors = $this->_getParam('error_handler'); 

    if (!$errors || !$errors instanceof ArrayObject) { 
     $this->view->message = 'You have reached the error page'; 
     return; 
    } 
    $redirect404 = false; 

    switch ($errors->type) { 
     case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE: 
     case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER: 
     case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION: 
      // 404 error -- controller or action not found 
      $httpCode = 404; 
      $this->getResponse()->setHttpResponseCode(404); 
      $redirect404 = true; 
      break;  
    ....   
    } 
    $this->getResponse()->setHttpResponseCode($httpCode); 
    $this->getResponse()->clearBody(); 
    ... 

    if (APPLICATION_ENV != 'development') { 
     if ($redirect404) 
      $this->_redirect('./../redirect404.html'); // with Static page 
     .... 
    } 

    // If you want a dynamic page use your error.phtml 
    // and use $this->view like any other controller 

3 - 在你的代码抛出的良好例外。

我希望它能帮助你

+0

当错误控制器完全能够使用相同内容呈现自己的模板时,为什么要重定向到静态HTML页面? –

+0

@TimFountain:你说的没错。 说到我在这里的项目,它是一个静态页面,所以我提出这个解决方案,但当然,控制器(我使用的错误不是404和403) – doydoy44