PHP学习心得


不可预知得内部异常处理

<h2>应用异常处理类</h2> <ul> <li>/app/ExceptionHandle.php</li> </ul> <pre><code class="language-php">// 自定义异常消息处理,处理异常可以把代码逻辑写在这个方法里边 render($request, Throwable $e);</code></pre> <h2>多应用时,使用应用异常处理类</h2> <ul> <li>控制器调用不存在的变量 <ul> <li>/app/admin/controller/Index.php</li> </ul></li> </ul> <pre><code class="language-php">/app/admin/controller/Index.php namespace app\admin\controller; use app\BaseController; use app\Request; class Index extends BaseController { /** * index.php/admin/Index/test */ public function test(Request $Request) { // $this-&gt;acb; throw new \think\exception\HttpException(404, '找不到相应的数据'); } }</code></pre> <h2>建立异常处理类</h2> <ul> <li>该文件主要参照/app/ExceptionHandle.php文件</li> <li>/app/admin/exception/Http.php</li> </ul> <pre><code class="language-php">/app/admin/exception/Http.php namespace app\admin\exception; use think\exception\Handle; use think\Response; use Throwable; class Http extends Handle{ public function render($request, Throwable $e): Response { $httpStatus = method_exists($e, 'getStatusCode') ? $e-&gt;getStatusCode() : 500; return return_data(config('statuscode.error'), $e-&gt;getMessage(), [], $httpStatus); } }</code></pre> <h2>容器Provider定义文件</h2> <ul> <li>/app/admin/provider.php</li> </ul> <pre><code class="language-php">return [ 'think\exception\Handle' =&gt; 'app\\admin\\exception\\Http', ];</code></pre> <h2>访问url</h2> <ul> <li>域名/index.php/admin/Index/test</li> </ul>

页面列表

ITEM_HTML