我读到 AppError 类现在是为了向后兼容,应该使用 Exceptions 来代替.如何为 404 错误或完全自定义错误等内容创建自定义错误页面?
试试这个:
/app/Config/core.php
异常渲染需要设置为AppExceptionRender.示例:
Configure::write('Exception', array('处理程序' =>'ErrorHandler::handleException','渲染器' =>'AppExceptionRenderer','日志' =>真的));/app/Controller/ErrorsController.php
class ErrorsController 扩展 AppController {公共 $name = '错误';公共函数 beforeFilter() {父::beforeFilter();$this->Auth->allow('error404');}公共函数 error404() {//$this->layout = 'default';}}/app/Lib/Error/AppExceptionRenderer.php
App::uses('ExceptionRenderer', 'Error');类 AppExceptionRenderer 扩展 ExceptionRenderer {公共函数 notFound($error) {$this->controller->redirect(array('controller' => 'errors', 'action' => 'error404'));}}/app/View/Errors/error404.ctp
<h2>404 错误 - 页面未找到</h2>在需要的地方插入:throw new NotFoundException();
I read that the AppError class is now for backwards compatibility and that Exceptions should be used instead. How does one go about creating custom error pages for things like 404 errors, or completely custom errors?
Try this:
/app/Config/core.php
Exception render need to set as an AppExceptionRender. Example:
Configure::write('Exception', array(
'handler' => 'ErrorHandler::handleException',
'renderer' => 'AppExceptionRenderer',
'log' => true
));
/app/Controller/ErrorsController.php
class ErrorsController extends AppController {
public $name = 'Errors';
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('error404');
}
public function error404() {
//$this->layout = 'default';
}
}
/app/Lib/Error/AppExceptionRenderer.php
App::uses('ExceptionRenderer', 'Error');
class AppExceptionRenderer extends ExceptionRenderer {
public function notFound($error) {
$this->controller->redirect(array('controller' => 'errors', 'action' => 'error404'));
}
}
/app/View/Errors/error404.ctp
<div class="inner404">
<h2>404 Error - Page Not Found</h2>
</div>
Insert it where you need: throw new NotFoundException();
这篇关于CakePHP 2.0 - 如何制作自定义错误页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
Zend 中的动作视图助手 - 解决方法?Action View Helper in Zend - Work around?(Zend 中的动作视图助手 - 解决方法?)
这是将 URI 与 PHP 中用于 MVC 的类/方法匹配的好方Is this a good way to match URI to class/method in PHP for MVC(这是将 URI 与 PHP 中用于 MVC 的类/方法匹配的好方法吗)
我在哪里保存 Zend Framework 中的部分(视图),以便Where do I save partial (views) in Zend Framework, to be accessible for all Views in my App?(我在哪里保存 Zend Framework 中的部分(视图),以
有一个网站的单一入口点.坏的?好的?没问题?Having a single entry point to a website. Bad? Good? Non-issue?(有一个网站的单一入口点.坏的?好的?没问题?)
MVC + 服务层在 Zend 或 PHP 中常见吗?Is MVC + Service Layer common in zend or PHP?(MVC + 服务层在 Zend 或 PHP 中常见吗?)
PHP MVC 方法中的 Hello World 示例Hello World example in MVC approach to PHP(PHP MVC 方法中的 Hello World 示例)