📅  最后修改于: 2020-10-26 05:13:46             🧑  作者: Mango
需要有效处理系统故障,以使系统平稳运行。 CakePHP带有默认的错误陷阱,可在发生错误时进行打印并记录错误。此错误处理程序用于捕获Exception 。
当调试为true时,错误处理程序将显示错误,而当调试为false时,错误处理程序将记录错误。 CakePHP具有许多异常类,并且内置的异常处理将捕获任何未捕获的异常并呈现一个有用的页面。
可以在文件config \ app.php中配置错误和异常。错误处理接受一些选项,这些选项使您可以为应用程序定制错误处理-
Option | Data Type | Description |
---|---|---|
errorLevel | int |
The level of errors you are interested in capturing. Use the built-in php error constants, and bitmasks to select the level of error you are interested in. |
trace | bool |
Include stack traces for errors in log files. Stack traces will be included in the log after each error. This is helpful for finding where/when errors are being raised. |
exceptionRenderer | string |
The class responsible for rendering uncaught exceptions. If you choose a custom class, you should place the file for that class in src/Error. This class needs to implement a render() method. |
log | bool |
When true, exceptions + their stack traces will be logged to Cake\Log\Log. |
skipLog | array |
An array of exception class names that should not be logged. This is useful to remove NotFoundExceptions or other common, but uninteresting logs messages. |
extraFatalErrorMemory | int |
Set to the number of megabytes to increase the memory limit by, when a fatal error is encountered. This allows breathing room to complete logging or error handling. |
如以下代码所示,在config / routes.php文件中进行更改。
config / routes.php
setRouteClass(DashedRoute::class);
$routes->scope('/', function (RouteBuilder $builder) {
$builder->registerMiddleware('csrf', new CsrfProtectionMiddleware([
'httpOnly' => true,
]));
$builder->applyMiddleware('csrf');
//$builder->connect('/pages',['controller'=>'Pages','action'=>'display', 'home']);
$builder->connect('/exception/:arg1/:arg2',
['controller'=>'Exps','action'=>'index'],
['pass' => ['arg1', 'arg2']]);
$builder->fallbacks();
});
在src / Controller / ExpsController.php中创建ExpsController.php文件。将以下代码复制到控制器文件中。
src / Controller / ExpsController.php
set('argument1',$arg1);
$this->set('argument2',$arg2);
if(($arg1 > 1 || $arg1 > 10) || ($arg2 < 1 || $arg2 > 10))
throw new Exception("One of the number is out of range [1-10].");
} catch(\Exception $ex){
echo $ex->getMessage();
}
}
}
?>
在src / Template上创建一个目录Exps ,并在该目录下创建一个名为index.php的View文件。将以下代码复制到该文件中。
src / Template / Exps / index.php
This is CakePHP tutorial and this is an example of Passed arguments.
Argument-1: =$argument1?>
Argument-2: =$argument2?>
通过访问以下URL执行以上示例。
http:// localhost / cakephp4 / exception / 5/0
执行后,您将收到以下输出。