lumen 屏蔽访问路由不存在

版权声明:本文为博主原创文章,转载请留言。 https://blog.csdn.net/root_miss/article/details/82836757
1、给web.php最后加
/**
 * 屏蔽错误路由
 */
$router->addRoute(['POST', 'GET'], '{url:.*}', function ($url) {
    return ['status' => -1, 'message' => $url . ' not exist'];
});

/**
 * 校验options
 */
$router->addRoute(['OPTIONS'], '{url:.*}', function () {
    return ['status' => 200, 'message' => 'ok'];
});

2、给控制器 Controller.php 加

/**
 * 返回错误
 * @param $errorCode
 * @param null $errorMsg
 * @return \Illuminate\Http\JsonResponse
 */
protected function error($errorCode, $errorMsg = null)
{
    if ($errorMsg === null) {
        $errorMsg = empty(Error::MSG[$errorCode]) ? '未定义错误 ' . $errorCode : Error::MSG[$errorCode];
    }
    $result = array(
        'code' => $errorCode,
        'msg' => $errorMsg,
    );
    return response()->json($result);
}

/**
 * 返回成功
 * @param $message
 * @param null $data
 * @return \Illuminate\Http\JsonResponse
 */
protected function success($message, $data = null)
{
    $res = array(
        'code' => 0,
        'msg' => $message,
    );
    if (isset($data)) {
        $res['data'] = $data;
    }
    return response()->json($res);

}

猜你喜欢

转载自blog.csdn.net/root_miss/article/details/82836757
今日推荐