gpt4 book ai didi

php - Cakephp 2报告级别0的错误

转载 作者:行者123 更新时间:2023-12-03 07:50:02 25 4
gpt4 key购买 nike

当报告级别设置为0且是 500错误而不是 404 时,我想显示一个自定义错误页面。似乎我无法在默认 View 之外访问错误消息。

我想在常规错误布局之外设置自定义布局。我知道如果将报告切换为1级或2级,那么它可以正常工作。我希望将其设置为0而不是出现400错误的生产环境。这可能吗?
$error->getMessage()

最佳答案

Q: I would like to show a custom error page when the reporting level is set to 0 and it is a 500 error



答:您可以编辑默认错误 View error400.ctp,也可以编辑 error500.ctp中的 /app/View/Errors/

Q: I would like to set a custom layout outside of the normal error layouts.



答:如果要使用其他(自定义)布局,则可以将文件 CakeErrorController.php/lib/Cake/Controller/复制到 /app/Controller/并在函数中添加以下行:

function __construct($request = null, $response = null) {
$this->layout = 'your-layout-name'; // add this line in the function
// ...
}

并像往常一样在 /app/View/Layouts/中添加 custom layout模板文件,例如 your-layout-name.ctp
如果您想在错误页面/布局中显示应用程序中的数据(例如,从数据库生成主菜单),则可以在错误 Controller 中添加更多自定义代码,例如:
// Error controller in /app/Controller/CakeErrorController.php
class CakeErrorController extends AppController {
public $uses = array('MenuCategory'); // load relevant table/model
public function __construct($request = null, $response = null) {
$this->set('mainmenu', $this->MenuCategory->getMenu()); // or find() or whatever
// ...
}
}

Q: How can I keep different error messages in production mode (debug level = 0)?



A 根据 documentation“默认情况下,ErrorHandler,在debug> 0时显示错误,在debug = 0时记录错误。”原因是您不应该向公众访问者(在生产模式下)显示诸如“缺少组件”之类的错误消息,因为该错误与访问者无关,并且对访问者无用(因此将响应404/500)。这些错误仅与开发相关,应在上线生产模式之前修复。

如果要更改此行为,则必须设置自己的错误处理,因为 book 中对 进行了说明。请直接在CakePHP code中查看说明。如果需要,您还可以创建自己的exeptions

如果您在使用/创建自己的错误处理程序时遇到问题,请在Stackoverflow上开始一个新问题,并提供更多详细信息:您创建了哪些文件(例如您的错误处理程序和/或异常),代码如何,代码如何?您是否尝试解决了问题...,并请给出您要实现的目标的清晰示例(例如,在我的答案的第一个版本的以下注释中,您正在谈论特殊的500个错误-触发这些错误的原因是什么?您要回复而不是500,还是要更改什么...)?

在某些情况下,替代解决方案也可能是这样的:

// In your controller
public function moderator_view($id = null) {
// Your error check, in this example the user role
if ( $this->Auth->user('role') != 'moderator' ) {
// Custom error message
$this->Flash->custom('You are not a moderator.');
// Redirect to your your special error page
// In this example got to previous page
return $this->redirect(
$this->referer(
// or go to a default page if no referrer is given
array('controller' => 'articles', 'action' => 'index', 'moderator' => false)
)
);
}
// else show moderator content...
}

还请查看CakePHP中自定义错误页面的many other questions ;-)

关于php - Cakephp 2报告级别0的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32525839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com