正常情况下,模块不存在会有报错信息,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;
最佳答案
