php 如何在 Zend Framework 中抛出 404 异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3815808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:12:29  来源:igfitidea点击:

How to throw 404 exceptions in Zend Framework

phpzend-framework

提问by Michael Ekoka

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,该插件具有可以用来匹配 Exception 类型并相应地处理它们的常量。例如

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?

有谁知道如何专门创建这些类型的异常?

回答by Vaidas Zilionis

You can do like this:

你可以这样做:

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

or

或者

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

回答by user2406735

You can do it like this:

你可以这样做:

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