2010-09-28 12 views
22

Sto cercando di utilizzare lo Zend_Controller_Plugin_ErrorHandler al trattamento dei miei casi di errore 404. In base allo doc, il plugin ha costanti che è possibile utilizzare per abbinare i tipi di eccezioni e gestirli di conseguenza. per esempio.Come buttare 404 eccezioni in Zend Framework

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 

Qualcuno sa come creare eccezioni di questi tipi in particolare?

risposta

68

Si può fare in questo modo:

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

o

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

per Zend 2 [guarda qui] (http: // stackoverflow.com/questions/15968331/zend-2-how-to-throw-a-404-error-from-controller) – Victor

3

Si può fare in questo modo:

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

Credo che il metodo corretto sia setHttpResponseCode non setStatusCode –