如何在Zend框架中抛出404异常

时间:2022-10-09 22:48:33

I'm trying to use the Zend_Controller_Plugin_ErrorHandler to handle my error 404 cases. According to the doc, the plugin has constants that one can use to match Exception types and handle them accordingly. e.g.

我正在尝试使用Zend_Controller_Plugin_ErrorHandler来处理我的404错误。根据doc,这个插件有一些常量可以用来匹配异常类型并相应地处理它们。如。

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

Does anyone know how to create exceptions of these types specifically?

有人知道如何特别创建这些类型的异常吗?

2 个解决方案

#1


69  

You can do like this:

你可以这样做:

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

or

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

#2


3  

You can do it like this:

你可以这样做:

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

#1


69  

You can do like this:

你可以这样做:

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

or

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

#2


3  

You can do it like this:

你可以这样做:

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