PHP版本:7.3.8
CakePHP版本:4.0.1
数据库服务器:10.4.6-MariaDB-mariadb.org二进制分发版
Web服务器:Apache / 2.4.39(Win64)OpenSSL / 1.1.1c
//我在做什么
我创建了一个错误记录功能来补充Cake随附的综合错误报告功能。开发的目的是帮助我减少在错误发生时隔离出我所花费的时间。我的应用程序上线的目的是为了与开发中的应用程序相同,同时也帮助我监视所有安全漏洞。
//发生了什么
数据库中记录了6到9个错误,但是记录该错误的函数位于语句的if部分中。
//用户控制器
public function view(string $userID = null): void
{
// Initialise the variables.
$this->args = func_num_args();
$this->params = func_get_args();
if ($this->args !== 1) {
echo 'in FAILED ' . '<br /><br />';
// THIS IS THE FUNCTION THAT RECORDS THE ERROR.
$this->setErrorLocation(
$this->controller(). ' - Line ' . __LINE__);
if ($this->recordError(
$this->getErrorLocation()) === false) {
throw new UnauthorizedException();
}
throw new UnauthorizedException();
}
else {
// BUT THIS IS WHATS PRINTED TO THE SCREEN.
echo 'in PASSED ' . '<br /><br />';
}
// Rest of view code...
}
执行上述代码时,将执行语句的else部分,并在屏幕上显示“ in PASSED”。这是预期的行为。
这是意外的行为:
================================================ ======================
//要复制的代码
我已经简化了代码,以期希望能够复制行为。
// APP控制器
//记录错误功能:
此功能背后的想法是最好的情况Scenerio我在数据库中获取日志,而Cake记录错误并引发异常,而最坏的情况是Cakecake错误并引发异常。
public function recordError(string $errorLocation): bool
{
// Initialise the variables.
$this->args = func_num_args();
$this->params = func_get_args();
// Check the number of arguments.
if ($this->args !== 1) {
return false;
}
// Check the argument type.
if (!is_string($this->params[0])) {
return false;
}
// Insert into the database.
$Errors = TableRegistry::getTableLocator()->get('Errors');
$error = $Errors->newEmptyEntity();
$error->ip = '::1';
$error->user_id = 1001; // NOTE: This must be a valid id so if $rules->existsIn is present in the user model the save does not fail.
$error->location = $this->params[0];
$error->date_occurred = '0000-00-00 00:00:00';
// Save the error.
if ($Errors->save($error)) {
return true;
}
return false;
}
//错误位置设置器和获取器。
protected function setErrorLocation(string $errorLocation): object
{
$this->errorLocation = $errorLocation;
return $this;
}
protected function getErrorLocation(): string
{
return $this->errorLocation;
}
//用户控制器
控制器名称
private function controller(): string
{
$val = 'UsersController';
return $val;
}
//数据库
Table name: errors
Name Type Collation Attributes Null Default
id Primary int(10) No None AUTO_INCREMENT
ip varchar(100) utf8mb4_unicode_ci No None
user_id int(10) No None
location varchar(200) utf8mb4_unicode_ci No None
date_occured datetime No None
================================================ ======================
//相关信息
在大多数情况下,此功能会按预期运行,但在控制器视图,编辑和索引方法中,会记录多个错误。
我在视图,编辑和索引中多次使用了此技术,并且仅在几个实例上无法正常工作。
不仅当我使用PHP参数函数时,当我使用is_string时,它还会在索引中记录多个错误。
我不确定这是我的环境,PHP,Cake还是我正在做的事情。
================================================ ======================
//我的问题
为什么将错误记录在数据库中?
(我的意思是,如何记录错误,而不会引发异常,并且在“通过”状态下打印到屏幕上?)
感谢Zenzs。
================================================ ======================
@Jeto
我使用了下面的backtrace函数,并显示以下内容:
$e = new \Exception;
var_dump($e->getTraceAsString());
string(3679)“
0 C:\ xampp \ htdocs \ crm \ vendor \ cakephp \ cakephp \ src \ Controller \ Controller.php(524):App \ Controller \ UsersController-> view('1026')
1个C:\ xampp \ htdocs \ crm \ vendor \ cakephp \ cakephp \ src \ Controller \ ControllerFactory.php(79):Cake \ Controller \ Controller-> invokeAction(Object(Closure),Array)
2 C:\ xampp \ htdocs \ crm \ vendor \ cakephp \ cakephp \ src \ Http \ BaseApplication.php(229):Cake \ Controller \ ControllerFactory-> invoke(Object(App \ Controller \ UsersController))>
3 C:\ xampp \ htdocs \ crm \ vendor \ cakephp \ cakephp \ src \ Http \ Runner.php(77):Cake \ Http \ BaseApplication-> handle(Object(Cake \ Http \ ServerRequest))>
4 C:\ xampp \ htdocs \ crm \ vendor \ cakephp \ authentication \ src \ Middleware \ AuthenticationMiddleware.php(122):Cake \ Http \ Runner-> handle(Object(Cake \ Http \ ServerRequest))>
5 C:\ xampp \ htdocs \ crm \ vendor \ cakephp \ cakephp \ src \ Http \ Runner.php(73):Authentication \ Middleware \ AuthenticationMiddleware-> process(Object(Cake \ Http \ ServerRequest),Object( Cake \ Http \ Runner))
6 C:\ xampp \ htdocs \ crm \ vendor \ cakephp \ cakephp \ src \ I18n \ Middleware \ LocaleSelectorMiddleware.php(70):Cake \ Http \ Runner-> handle(Object(Cake \ Http \ ServerRequest))
7 C:\ xampp \ htdocs \ crm \ vendor \ cakephp \ cakephp \ src \ Http \ Runner.php(73):Cake \ I18n \ Middleware \ LocaleSelectorMiddleware-> process(Object(Cake \ Http \ ServerRequest),对象(Cake \ Http \ Runner)
8 C:\ xampp \ htdocs \ crm \ vendor \ cakephp \ cakephp \ src \ Http \ Runner.php(77):Cake \ Http \ Runner-> handle(Object(Cake \ Http \ ServerRequest))>
9 C:\ xampp \ htdocs \ crm \ vendor \ cakephp \ cakephp \ src \ Http \ Middleware \ CsrfProtectionMiddleware.php(132):Cake \ Http \ Runner-> handle(Object(Cake \ Http \ ServerRequest))
10个C:\ xampp \ htdocs \ crm \ vendor \ cakephp \ cakephp \ src \ Http \ Runner.php(73):Cake \ Http \ Middleware \ CsrfProtectionMiddleware-> process(Object(Cake \ Http \ ServerRequest),对象(Cake \ Http \ Runner)
11 C:\ xampp \ htdocs \ crm \ vendor \ cakephp \ cakephp \ src \ Http \ Runner.php(58):Cake \ Http \ Runner-> handle(Object(Cake \ Http \ ServerRequest))>
12 C:\ xampp \ htdocs \ crm \ vendor \ cakephp \ cakephp \ src \ Routing \ Middleware \ RoutingMiddleware.php(162):Cake \ Http \ Runner-> run(Object(Cake \ Http \ MiddlewareQueue),对象(Cake \ Http \ ServerRequest),对象(Cake \ Http \ Runner)
13 C:\ xampp \ htdocs \ crm \ vendor \ cakephp \ cakephp \ src \ Http \ Runner.php(73):Cake \ Routing \ Middleware \ RoutingMiddleware-> process(Object(Cake \ Http \ ServerRequest),对象(Cake \ Http \ Runner)
14 C:\ xampp \ htdocs \ crm \ vendor \ cakephp \ cakephp \ src \ Routing \ Middleware \ AssetMiddleware.php(68):Cake \ Http \ Runner-> handle(Object(Cake \ Http \ ServerRequest)
15 C:\ xampp \ htdocs \ crm \ vendor \ cakephp \ cakephp \ src \ Http \ Runner.php(73):Cake \ Routing \ Middleware \ AssetMiddleware-> process(Object(Cake \ Http \ ServerRequest),对象(Cake \ Http \ Runner)
16 C:\ xampp \ htdocs \ crm \ vendor \ cakephp \ cakephp \ src \ Error \ Middleware \ ErrorHandlerMiddleware.php(118):Cake \ Http \ Runner-> handle(Object(Cake \ Http \ ServerRequest))
17 C:\ xampp \ htdocs \ crm \ vendor \ cakephp \ cakephp \ src \ Http \ Runner.php(73):Cake \ Error \ Middleware \ ErrorHandlerMiddleware-> process(Object(Cake \ Http \ ServerRequest),对象(Cake \ Http \ Runner)
18 C:\ xampp \ htdocs \ crm \ vendor \ cakephp \ debug_kit \ src \ Middleware \ DebugKitMiddleware.php(60):Cake \ Http \ Runner-> handle(Object(Cake \ Http \ ServerRequest))>
19 C:\ xampp \ htdocs \ crm \ vendor \ cakephp \ cakephp \ src \ Http \ Runner.php(73):DebugKit \ Middleware \ DebugKitMiddleware-> process(Object(Cake \ Http \ ServerRequest),Object( Cake \ Http \ Runner))
20 C:\ xampp \ htdocs \ crm \ vendor \ cakephp \ cakephp \ src \ Http \ Runner.php(58):Cake \ Http \ Runner-> handle(Object(Cake \ Http \ ServerRequest))>
21 C:\ xampp \ htdocs \ crm \ vendor \ cakephp \ cakephp \ src \ Http \ Server.php(90):Cake \ Http \ Runner-> run(Object(Cake \ Http \ MiddlewareQueue),Object( Cake \ Http \ ServerRequest),对象(App \ Application))
22 C:\ xampp \ htdocs \ crm \ webroot \ index.php(40):Cake \ Http \ Server-> run()
23 {main}“
XDebug是一个很好的建议,但是到目前为止,我还没有安装它,但是它在我的清单上。
此评论有助于我进一步了解:
可能很容易是某些调用属于错误情况而其中一个通过了。
谢谢。
================================================ ======================
@ndm
我正在使用Chrome,它正在发出多个请求,但仅针对我应用程序头部的请求。除了应有的请求外,我看不到其他任何请求。 IE:请求以下内容,因为以下所有内容均存储在本地:
<script src="bootstrap-sass/javascripts/jquery.min.js"></script>
<script src="bootstrap-sass/javascripts/bootstrap.min.js"></script>
<script src="bootstrap-sass/javascripts/crm-sys.js"></script>
<link rel="stylesheet "href="bootstrap-sass/stylesheets/all.css" type="text/css" media="screen"/>
<link rel="stylesheet" href="bootstrap-sass/stylesheets/sys-styles.css" media="screen" type="text/css"/>
由于我安装的Compass符合我的要求,因此我的webroot结构略有不同。 IE:
webroot
->bootstrap-sass
->.sass-cache
->fonts
->javascripts
->sass
->stylesheets
->webfonts
config.rb
->css
->font
->img
->js
IE:我不将样式表存储在Cake的默认css文件夹中。我不确定是否可以重新编译它,但是我认为我会在上线之前保存该工作。
可能的答案:
Chrome浏览器不会向不应该发送的内容发出请求,但是由于我使用指南针,因此必须遍历目录以确保可以在索引,添加,查看和编辑中找到文件,并且Chrome确认它发出了多个请求相同的文件。
我必须像下面一样声明3次以上的每个链接,然后Chrome确认它对每个文件发出3个请求。
<!-- Welcome and index etc -->
<script src="bootstrap-sass/javascripts/jquery.min.js"></script>
<!-- Add -->
<script src="../bootstrap-sass/javascripts/jquery.min.js"></script>
<!-- View and edit -->
<script src="../../bootstrap-sass/javascripts/jquery.min.js"></script>
这可能是我所看到的行为的原因吗?
我想可能是因为数据库中的多个条目始终是额外的3、6、9。即:总是分成三组,我偶然地对每个文件声明了3次,并且用户代理确认了,这有点巧合它发出了3个请求。
肯定是这样。你同意吗?
也要感谢您注意不要回传控制器中的数据。我能确认你的意思是我应该写一个平面文件,甚至是数据库。
谢谢。