php CakePHP 2.0 - 如何制作自定义错误页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9620363/
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
CakePHP 2.0 - How to make custom error pages?
提问by BadHorsie
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?
我读到 AppError 类现在是为了向后兼容,应该使用 Exceptions 来代替。如何为 404 错误或完全自定义错误等内容创建自定义错误页面?
回答by Andrew Kulakov
Try this:
尝试这个:
/app/Config/core.php
/app/Config/core.php
Exception render need to set as an AppExceptionRender
. Example:
异常渲染需要设置为AppExceptionRender
. 例子:
Configure::write('Exception', array(
'handler' => 'ErrorHandler::handleException',
'renderer' => 'AppExceptionRenderer',
'log' => true
));
/app/Controller/ErrorsController.php
/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/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
/app/View/Errors/error404.ctp
<div class="inner404">
<h2>404 Error - Page Not Found</h2>
</div>
Insert it where you need: throw new NotFoundException();
将其插入您需要的位置: throw new NotFoundException();
回答by bfncs
To customize the content of a 404-error page and don't need custom logic, simply edit the contents of app/View/Errors/error400.ctp
.
要自定义 404 错误页面的内容并且不需要自定义逻辑,只需编辑app/View/Errors/error400.ctp
.
This seems not to be documented properly anywhere.
这似乎没有在任何地方正确记录。
回答by user221931
If you're only looking to use another layout instead of the default, just add $this->layout = 'your_error_layout';
inside your error400.ctp (or any other error page you create under View/Errors).
如果您只想使用其他布局而不是默认布局,只需$this->layout = 'your_error_layout';
在您的 error400.ctp(或您在 View/Errors 下创建的任何其他错误页面)中添加。
回答by Cake PHP
Create a layout with name 404 or anything and use in app controller
创建名称为 404 或任何名称的布局并在应用程序控制器中使用
function _setErrorLayout() {
if ($this->name == 'CakeError') {
$this->layout = '404';
}
}
function beforeRender () {
$this->_setErrorLayout();
}
回答by Daniel Faria
The accepted answer is not the best option because they redirect the url of your browser to http://example.com.br/error/error404
and the user can't follow what page he inputed to generate this error.
接受的答案不是最佳选择,因为它们会将您的浏览器的 url 重定向到http://example.com.br/error/error404
,并且用户无法按照他输入的页面来生成此错误。
The better way to handle this situation is edit file on View/Errors/error400.ctp
, so when you input a not found url like http:example.com/crazy-wrong-url
, the browser will keep this url but render the content of the error400.ctp
file that you edit.
处理这种情况的更好方法是在 上编辑文件View/Errors/error400.ctp
,因此当您输入一个未找到的 url 时http:example.com/crazy-wrong-url
,浏览器将保留此 url,但会呈现error400.ctp
您编辑的文件的内容。
If you want change the layout that the view will be rendered you can type this in your view <?php $this->layout = 'error'; ?>
如果您想更改将呈现视图的布局,您可以在您的视图中输入 <?php $this->layout = 'error'; ?>
回答by Jorge Junior
Had discovered today that is possible to get excpetion code in ctp file using $error->getCode()
(in CakePHP 2.x, at least).
今天发现可以使用$error->getCode()
(至少在 CakePHP 2.x 中)获取 ctp 文件中的 excpetion 代码。
The $error
is were Cake puts the exception object...
该$error
信息是对蛋糕放异常对象...
Now you should be able to change the content of your view with an if/else
conditional block, based in this value.
现在,您应该能够if/else
根据此值使用条件块更改视图的内容。
回答by Jonas Millan
You can create CustomErrorPages
or Exeptions
by simply creating a class
of your error your going to show. This class needs to extend CakeExeption
. Then build your exeptionlogic and your set. Now you can just throw new <YourExeptionClass>()
and it will display an error.
您可以创建CustomErrorPages
或Exeptions
简单地创建class
您要显示的错误。这个类需要扩展CakeExeption
。然后构建您的 exeptionlogic 和您的集合。现在你可以了throw new <YourExeptionClass>()
,它会显示一个错误。
Documentation: CakeExceptions