laravel访问不存在的路由跳转到自定义的错误页面

//找到app/Exceptions/Handler.php打开,添加下面代码,记得先use一下
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $exception
 * @return \Illuminate\Http\Response
 */
public function render($request, Exception $exception)
{

    // 如果不被允许的路由
    if ($exception instanceof MethodNotAllowedHttpException || $exception instanceof NotFoundHttpException) {
        if (!($request->ajax() || $request->wantsJson())) {
            return response()->view('error.404');
        }
    }
    return parent::render($request, $exception);
}

阅读更多

没有更多推荐了,返回首页