thinkphp5.0 空模塊、空控制器、空方法


空模塊

'exception_handle'       => function(Exception $e){
        // 參數驗證錯誤
        if ($e instanceof \think\exception\ValidateException) {
            return json($e->getError(), 422);
        }

        // 請求異常
        if ($e instanceof \think\exception\HttpException && request()->isAjax()) {
            return response($e->getMessage(), $e->getStatusCode());
        }
        return redirect(url('home/index/index'));//重定向至自定義錯誤提示頁面
    },

或者

'exception_handle'       => '\\app\\common\\exception\\Http',
<?php
namespace app\common\exception;
use Exception;
use think\exception\Handle;
use think\exception\HttpException;
class Http extends Handle
{

    public function render(Exception $e)
    {
        // 參數驗證錯誤
        if ($e instanceof ValidateException) {
            return json($e->getError(), 422);
        }

        // 請求異常
        if ($e instanceof HttpException && request()->isAjax()) {
            return response($e->getMessage(), $e->getStatusCode());
        }
        
        //TODO::開發者對異常的操作
        //可以在此交由系統處理
        return redirect(url('home/index/index'));//重定向至自定義錯誤提示頁面 //return parent::render($e);
    }

}

?>

空控制器

// 默認的空控制器名
    'empty_controller'       => 'Error',
<?php
namespace app\home\controller;
use think\Controller;
use think\Request;

class Error extends Controller
{
    public function index(Request $request)
    {
        $this->redirect(url('home/index/index'));//空控制器處理       
    }
}

空方法

public function _empty(){
        $this->redirect(url('home/index/index'));//空方法處理
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM