问题描述
我在CakePHP网站上有以下代码作为我的AppError文件:
<?php
class AppError extends ErrorHandler
{
function _outputMessage($ template)
{
$ this-> controller-> render($ template,'error');
$ this-> controller-> afterFilter();
echo $ this-> controller-> output;
}
function error404()
{
$ this-> set('title_for_layout','Not Found(404)');
}
}
?>
第一个函数通过使所有错误使用错误布局而不是默认布局正常工作。但第二个功能导致应用程序炸毁...为什么?
感谢
它扩展类,AppError继承所有的方法ErrorHandler.So如果你没有一个方法error404(),AppError对象将调用其父方法,在这种情况下error404在ErrorHandler; / p>
但是,如果你定义已经在ErrorHandler中退出的方法,它不会'扩展'但是'覆盖它'
换句话说:方法error404,并且此方法由AppError对象调用它将调用它的版本error404()
现在,如果你看ErrorH4中的error404函数
函数error404($ params){
extract($ params);
if(!isset($ url)){
$ url = $ action;
}
if(!isset($ message)){
$ message ='';
}
if(!isset($ base)){
$ base ='';
}
标题(HTTP / 1.0 404 Not Found);
$ this-> error(array('code'=>'404',
'name'=>'Not found',
'message'=> sprintf 请求的地址%s在此服务器上找不到。,$ url,$ message),
'base'=> $ base)
exit();
}
你可以看到在这个函数中有一定的行为和参数。所以这就是为什么你的应用程序崩溃,在某个地方AppError调用error404期望从父(ErrorHandler)类的行为。尝试模仿该方法(通过包括参数和退出在结束)。
$ this-> set('title_for_layout','Not Found(404)');
我认为应该是
$ this-> controller-> set('title_for_layout','Not Found(404)');`
I have the following code as my AppError file on my CakePHP site:
<?php
class AppError extends ErrorHandler
{
function _outputMessage($template)
{
$this->controller->render($template, 'error');
$this->controller->afterFilter();
echo $this->controller->output;
}
function error404 ()
{
$this->set('title_for_layout', 'Not Found (404)');
}
}
?>
The first function works fine by making all errors use the error layout instead of the default one. But the second function causes the App to blow up... why? Shouldn't it just extend the error404 function inside the error handler?
Thanks
It extends the class and by that AppError inherits all the methods from ErrorHandler.So if you don't have a method error404() , the AppError object will call its parent method, in this case error404 in ErrorHandler;
However, if you define the method that already exits in ErrorHandler it will not 'extend it' but 'override it.'In other words: If you create method error404 and this method is called by AppError object it will call its version of error404()
Now, if you look at error404 function inside ErrorHandler
function error404($params) {
extract($params);
if (!isset($url)) {
$url = $action;
}
if (!isset($message)) {
$message = '';
}
if (!isset($base)) {
$base = '';
}
header("HTTP/1.0 404 Not Found");
$this->error(array('code' => '404',
'name' => 'Not found',
'message' => sprintf("The requested address %s was not found on this server.", $url, $message),
'base' => $base));
exit();
}
you can see that there is certain behavior and params are expected in this function. So that's why your app is crashing, somewhere AppError is calling error404 expecting the behavior from parent (ErrorHandler) class.Try to mimic the method (by including params and exit at the end).
Also $this->set('title_for_layout', 'Not Found (404)');
I think it should be
$this->controller->set('title_for_layout', 'Not Found (404)');`
这篇关于CakePHP AppError扩展函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!