CakePHP 错误和异常处理

WBOY
发布: 2024-09-10 17:26:09
原创
627 人浏览过

系统故障需要得到有效的处理,以保证系统的顺利运行。 CakePHP 带有默认的错误捕获,当发生错误时打印并记录错误。同样的错误处理程序用于捕获异常

错误处理程序在 debug 为 true 时显示错误,并在 debug 为 false 时记录错误。 CakePHP 有许多异常类,内置的异常处理将捕获任何未捕获的异常并呈现有用的页面。

错误和异常配置

错误和异常可以在文件configapp.php中配置。错误处理接受一些选项,允许您为您的应用程序定制错误处理 -

表>
<?php namespace App\Controller;
   use App\Controller\AppController;
   use Cake\Core\Exception\Exception;
   class ExpsController extends AppController {
      public function index($arg1,$arg2) {
         try{
            $this->set('argument1',$arg1);
            $this->set('argument2',$arg2);
            if(($arg1 > 1 || $arg1 > 10) || ($arg2  10))
               throw new Exception("One of the number is out of range [1-10].");
         } catch(\Exception $ex){
            echo $ex->getMessage();
         }
      }
   }
?>
登录后复制
示例

config/routes.php 文件中进行更改,如以下代码所示。 config/routes.php

src/Controller/ExpsController.php 创建
This is CakePHP tutorial and this is an example of Passed arguments.<br>
Argument-1: =$argument1?><br>
Argument-2: =$argument2?><br>
登录后复制
ExpsController.php

文件。

将以下代码复制到控制器文件中。

src/Controller/ExpsController.php

CakePHP 错误和异常处理在src/Template处创建一个目录Exps,并在该目录下创建一个名为index.php的View文件。将以下代码复制到该文件中。 src/Template/Exps/index.php 通过访问以下 URL 来执行上述示例。 http://localhost/cakephp4/Exception/5/0 输出 执行后,您将收到以下输出。
选项 数据类型 描述
错误级别 int
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 CakeLogLog.

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.

您有兴趣捕获的错误级别。使用内置的 php 错误常量和位掩码来选择您感兴趣的错误级别。
跟踪 布尔

在日志文件中包含错误的堆栈跟踪。每次错误后,堆栈跟踪将包含在日志中。这有助于查找发生错误的位置/时间。

异常渲染器 字符串

负责渲染未捕获异常的类。如果您选择自定义类,则应将该类的文件放置在

src/Error

中。该类需要实现 render() 方法。

日志 布尔
<?php use Cake\Http\Middleware\CsrfProtectionMiddleware;
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;
$routes->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();
});
登录后复制
当为 true 时,异常及其堆栈跟踪将被记录到

CakeLogLog

跳过日志 数组 不应记录的异常类名称数组。这对于删除 NotFoundExceptions 或其他常见但无趣的日志消息很有用。

额外的致命错误内存 int 设置为在遇到致命错误时增加内存限制的兆字节数。这为完成日志记录或错误处理提供了喘息的空间。

以上是CakePHP 错误和异常处理的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!