欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

zend framework 报错定义页面 博客分类: 关于zend framework zendfreemarker 

程序员文章站 2024-03-20 08:02:09
...
确保index.php中有如下内容:
$plugin = new Zend_Controller_Plugin_ErrorHandler();
$plugin->setErrorHandlerModule('frontController')
->setErrorHandlerController('Error')
->setErrorHandlerAction('error');

$frontController = Zend_Controller_Front::getInstance();//取得Zend_Controller_Front类实例
$frontController->registerPlugin($plugin);
$frontController->setControllerDirectory (array(
"default" => 'application/controllers',
)); // 指定一组控制器文件路径,参数是数组。设置前端控制器,主要是让控制器知道我们的的控制器类在哪里
$frontController->throwExceptions(false);//让前端控制器抛出所有的异常。 默认情况下,前端控制器将捕获这些异常并将其人传递到 ErrorController 控制器,在开发过程,这样错误容易发现。但在实际运行中,不能将这些错误直接显示给前端用户。
$frontController->setParam('noViewRenderer', true);// 指明不使用视图,false 是默认值。
$frontController->setParam('noErrorHandler', false);
$frontController->dispatch(); //程序执行分发
新建一个控制器ErrorController.php
require_once 'Zend/Controller/Action.php';
//默认错误控制器类
class ErrorController extends Zend_Controller_Action{
public function init()
{
$this->registry = Zend_Registry::getInstance();
$this->view = $this->registry['view'];
$this->view->baseUrl = $this->_request->getBaseUrl();
}
/**
* This action handles
* - Application errors
* - Errors in the controller chain arising from missing
* controller classes and/or action methods
*/
public function errorAction(){
//动作方法名:对应的文件是views/scripts/error/error.phtml
$errors = $this->_getParam('error_handler');
switch ($errors->type) {
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
// 404 error -- controller or action not found
//$this->getResponse()->setRawHeader('HTTP/1.1 404 Not Found!!!');//当这行起作用时候,在ie6下会出现它自身默认的404显示。
$content= 'The page you request was not found! ';
break;
default:
// application error; display error page, but don't change
// status code
$content = 'Application Error';
break;
}
$this->getResponse()->clearbody();
$this->view->message = $content;
echo $this->view->render('error/error.phtml');//显示增加模版
}
}
新建错误视图application/views/scripts/error/error.phtml
<?php echo $this->message;?>Please <a href="<?php echo $this->baseUrl; ?>">GoBack</a>
相关标签: zend freemarker