2010-09-28 17 views
22

我試圖使用Zend_Controller_Plugin_ErrorHandler來處理我的錯誤404個案例。 根據doc,插件具有可用於匹配Exception類型並相應處理它們的常量。例如如何在Zend Framework中拋出404異常

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 

有沒有人知道如何創建這些類型的例外具體?

回答

68

你可以這樣做:

$this->getResponse()->setHttpResponseCode(404); 

throw new Zend_Controller_Action_Exception('This page does not exist', 404); 
+0

對於Zend的2 [看這裏(HTTP:// stackoverflow.com/questions/15968331/zend-2-how-to-throw-a-404-error-from-controller) – Victor 2014-09-11 15:37:44

3

你可以這樣說:

$this->getResponse()->setStatusCode(404); 
return; 
+1

我相信正確的方法是setHttpResponseCode not setStatusCode – 2016-02-15 14:51:30