ThinkPHP5提示模块不存在解决思路

浏览:31095 发布日期:2017/08/29 分类:ThinkPHP5专区 关键字: thinkphp5 模块不存在
这里提供一个模块不存在的解决思路
正常情况下,模块不存在会有报错信息,thinkphp能捕捉到module not exists:xxx的异常,我看官方文档有解决空操作的方法,但是用户访问空模块的解决方法我好像没看到,欢迎大牛指正。
参考文档https://www.kancloud.cn/manual/thinkphp5/126075

我们自定义exception的处理类,详情见参考文档,然后对官方文档稍作修改,很容易实现自己的需求
上代码<?php

namespace app\common\exception;

use Exception;
use think\exception\Handle;
use think\exception\HttpException;
use think\Response;

class Http extends Handle {
    
    public function render (Exception $e)
    {
        if ( $e instanceof HttpException ) {
            $statusCode = $e->getStatusCode ();
            
            if ( stristr ($e->getMessage (), "module not exists:") ) {
                return Response::create ("<script>window.location.href='http://{$_SERVER[ 'HTTP_HOST' ]}';</script>", "html")->send ();
            }
        }
        
        //可以在此交由系统处理
        return parent::render ($e);
    }
    
}
这里是对于不存在模块直接跳转到网站首页

~~end;



最佳答案
评论( 相关
后面还有条评论,点击查看>>