laravel 自定义异常处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29824808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel custom exception handling
提问by uday8486
I am new to Laravel framework. I need to add a custom exception error handler. I tried to add below code to my global.php
file:
我是 Laravel 框架的新手。我需要添加一个自定义异常错误处理程序。我尝试将以下代码添加到我的global.php
文件中:
App::error(function(CustomException $exception, $code)
{
echo 'Debug: CustomException<br/>';
});
To throw this error I used below code in one of my controllers:
为了抛出这个错误,我在我的一个控制器中使用了下面的代码:
throw new CustomException();
But I am getting error as CustomException not found.
但由于未找到 CustomException,我收到错误消息。
I googled it for solution, but everywhere I find the same solution.
我在谷歌上搜索了解决方案,但到处都找到了相同的解决方案。
Please help me to get this fixed.
请帮我解决这个问题。
回答by laurent
You need to define your custom exception first. So at the top of your global.php
file, add this:
您需要先定义自定义异常。因此,在您的global.php
文件顶部,添加以下内容:
class CustomException extends Exception {}
Of course, you can add custom properties and methods to this class as needed.
当然,您可以根据需要向该类添加自定义属性和方法。
回答by Eduardo Stuart
You can create your class (Ex.: app/Exceptions/MyCustomException.php), then add to composer.json autoload files.
您可以创建您的类(例如:app/Exceptions/MyCustomException.php),然后添加到 composer.json 自动加载文件。
"autoload": {
"files":["app/Exceptions/MyCustomException.php]
}
Then: run a composer dumpautoload
然后:运行一个 composer dumpautoload
Now, you can use your MyCustomException
class.
现在,您可以使用您的MyCustomException
课程。
--
——
<?php
class MyCustomException extends \Exception {}