PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > Laravel教程 > 正文

    Laravel自定义错误、自定义异常类提示

    作者:admin来源:网络浏览:时间:2021-02-23 18:21:35我要评论
    导读:Laravel自定义错误异常类代码分享,希望对你有所帮助
    方法一#
    新增CustomException.php文件#
    App\Exceptions\CustomException.php

    1. <?php 
    2.  
    3. namespace App\Exceptions; 
    4.  
    5. use Exception; 
    6. /** 
    7.  * 自定义异常基类 
    8.  * Class BaseException 
    9.  * @package App\Exceptions 
    10.  */ 
    11. class CustomException extends Exception 
    12.     /** 
    13.      * 状态码 
    14.      * @var int|mixed 
    15.      */ 
    16.     public $code = 200; 
    17.  
    18.     /** 
    19.      * 错误具体信息 
    20.      * @var mixed|string 
    21.      */ 
    22.     public $message = 'json'
    23.  
    24.     /** 
    25.      * 构造函数,接收关联数组 
    26.      * BaseException constructor. 
    27.      * @param array $params 
    28.      */ 
    29.     public function __construct($params = []) 
    30.     { 
    31.         parent::__construct(); 
    32.         if (!is_array($params)) { 
    33.             return ; 
    34.         } 
    35.         if (array_key_exists('code', $params)) { 
    36.             $this->code = $params['code']; 
    37.         } 
    38.         if (array_key_exists('msg', $params)) { 
    39.             $this->message = $params['msg']; 
    40.         } 
    41.     } 
    42.  
    43.     public function report() 
    44.     { 
    45.         // 
    46.     } 
    47.     public function render($request) 
    48.     { 
    49.         $result = [ 
    50.             'code'  => $this->code, 
    51.             'msg'   => $this->message, 
    52.         ]; 
    53.         //记录日志 
    54. //        Log::error($this->message); 
    55.         if($request->ajax()){ 
    56.             return response()->json($result)->setEncodingOptions(JSON_UNESCAPED_UNICODE); 
    57.         }else
    58.             $params = [ 
    59.                 'msg'  => $this->message, 
    60.                 'wait' => 3, 
    61.                 'url'  => 'javascript:history.back(-1);'
    62.             ]; 
    63.             return response()->view('common.error', $params, 500); 
    64.         } 
    65.     } 

    方法二#
    1.新增CustomException.php文件#
    App\Exceptions\CustomException.php
     

    1. <?php 
    2.  
    3. namespace App\Exceptions; 
    4.  
    5. /** 
    6.  * 自定义异常基类 
    7.  * Class BaseException 
    8.  * @package App\Exceptions\Custom 
    9.  */ 
    10. class CustomException extends \Exception 
    11.     /** 
    12.      * 状态码 
    13.      * @var int|mixed 
    14.      */ 
    15.     public $code = 200; 
    16.  
    17.     /** 
    18.      * 错误具体信息 
    19.      * @var mixed|string 
    20.      */ 
    21.     public $message = 'json'
    22.  
    23.     /** 
    24.      * 构造函数,接收关联数组 
    25.      * BaseException constructor. 
    26.      * @param array $params 
    27.      */ 
    28.     public function __construct($params = []) 
    29.     { 
    30.         parent::__construct(); 
    31.         if (!is_array($params)) { 
    32.             return ; 
    33.         } 
    34.         if (array_key_exists('code', $params)) { 
    35.             $this->code = $params['code']; 
    36.         } 
    37.         if (array_key_exists('msg', $params)) { 
    38.             $this->message = $params['msg']; 
    39.         } 
    40.     } 
    41.  
    42.  

    2.修改render()方法#
    App\Exceptions\Handler.php

    1. public function render($request, Exception $exception) 
    2.     { 
    3.         if ($exception instanceof CustomException) { 
    4.             // 如果是自定义的异常 
    5.             $this->code    = $exception->code; 
    6.             $this->message = $exception->message; 
    7.             $result = [ 
    8.                 'code'  => $this->code, 
    9.                 'msg'   => $this->message, 
    10.             ]; 
    11.             //记录日期 
    12.             Log::error($exception->message); 
    13.             if($request->ajax()){ 
    14.                 return response()->json($result)->setEncodingOptions(JSON_UNESCAPED_UNICODE); 
    15.             }else
    16.                 $params = [ 
    17.                     'msg'  => $this->message, 
    18.                     'wait' => 3, 
    19.                     'url'  => 'javascript:history.back(-1);'
    20.                 ]; 
    21.                 return response()->view('common.error', $params, 500); 
    22.             } 
    23.         } 
    24.          
    25.         return parent::render($request, $exception); 
    26.     } 

    测试#
     

    1. throw  new \App\Exceptions\CustomException(['msg'=>'自定义错误','code'=>400]); 

     

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-153-6503-1.html