laravel路由问题

laravel HTTP路由可带正则表达式验证,不符合规则的会报错,这样不友好,该如何设置报错信息?

Route::getuser/{name}, function$name
{ //
}
->wherename, [A-Za-z]+; Route::getuser/{id}, function$id
{ //
}
->whereid, [0-9]+;

php/** * Render the given HttpException. * * @param SymfonyComponentHttpKernelExceptionHttpException $e * @return SymfonyComponentHttpFoundationResponse */ protected function renderHttpExceptionHttpException $e { if view->existserrors..$e->getStatusCode { return response->viewerrors..$e->getStatusCode, [], $e->getStatusCode; } else { return new SymfonyDisplayerconfigapp.debug->createResponse$e; } } 比如你想自定义 404 错误页面的话,只要创建一个 resources/views/errors/404.blade.php 的视图文件 404内容自定义了

线上debug是必须关闭的,而且可以自定义404页面

NotFoundHttpException 异常,在 app/Exceptions/Handler 里捕获一下

<?php namespace AppExceptions; use Exception;
use IlluminateFoundationExceptionsHandler as ExceptionHandler;
use SymfonyComponentHttpKernelExceptionHttpException;
use SymfonyComponentHttpKernelExceptionMethodNotAllowedHttpException;
use SymfonyComponentHttpKernelExceptionNotFoundHttpException; class Handler extends ExceptionHandler
{ /** * A list of the exception types that should not be reported. * * @var array */ protected $dontReport = [ //SymfonyComponentHttpKernelExceptionHttpException ]; /** * Report or log an exception. * * This is a great spot to send exceptions to Sentry, Bugsnag, etc. * * @param Exception $e * * @return void */ public function reportException $e { return parent::report$e; } /** * Render an exception into an HTTP response. * * @param IlluminateHttpRequest $request * @param Exception $e * * @return IlluminateHttpResponse */ public function render$request,Exception $e { if$e instanceof NotFoundHttpException { return Response::viewsys::missing,[],404; //你需要的在这里,404 } //其他一些异常 照着写就行了 //... }
} 

直接放个404页面到errors文件夹即可。

发表评论

电子邮件地址不会被公开。 必填项已用*标注