Laravel 11自定义错误页面处理:区分后台与前台错误页
Laravel 11 区分前后台自定义错误页面实现方案
Laravel 11 移除了默认的 app/Exceptions/Handler.php 文件,改为通过 bootstrap/app.php 中的 withExceptions 方法配置异常处理。以下是实现前后台独立错误页面的两种方式:
方式一:直接在 bootstrap/app.php 中配置
打开项目根目录下的 bootstrap/app.php,在应用配置链中添加 withExceptions 闭包,嵌入异常判断逻辑:
return Application::configure(basePath: dirname(__DIR__)) ->withRouting( web: __DIR__.'/../routes/web.php', commands: __DIR__.'/../routes/console.php', health: '/up', ) ->withMiddleware(function (Middleware $middleware) { // 你的中间件配置 }) ->withExceptions(function (Exceptions $exceptions) { // 处理4xx/5xx NotFound类异常 $exceptions->render(function (\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $exception, $request) { $statusCode = $exception->getStatusCode(); // 后台路由(admin前缀)匹配 if ($request->is(['admin', 'admin/*'])) { $adminViews = [ 400 => 'admin.errors.400', 403 => 'admin.errors.403', 404 => 'admin.errors.404', 500 => 'admin.errors.500', 503 => 'admin.errors.503', ]; if (isset($adminViews[$statusCode])) { return response()->view($adminViews[$statusCode], [], $statusCode); } } // 前台404页面 if ($statusCode === 404) { return response()->view("customer.errors.404", [], 404); } }); // 如需处理其他异常(如403权限异常),可添加额外renderable闭包 $exceptions->render(function (\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException $exception, $request) { if ($request->is(['admin', 'admin/*'])) { return response()->view("admin.errors.403", [], 403); } // 前台403页面逻辑 return response()->view("customer.errors.403", [], 403); }); }) ->create();
方式二:自定义异常处理类(推荐,代码更易维护)
- 创建自定义Handler类
在app/Exceptions目录下新建Handler.php文件,继承框架默认的异常处理器并注册自定义渲染逻辑:
<?php namespace App\Exceptions; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; class Handler extends ExceptionHandler { /** * 注册异常处理回调 */ public function register(): void { // 处理NotFound类异常 $this->renderable(function (NotFoundHttpException $exception, $request) { $statusCode = $exception->getStatusCode(); if ($request->is(['admin', 'admin/*'])) { $adminViews = [ 400 => 'admin.errors.400', 403 => 'admin.errors.403', 404 => 'admin.errors.404', 500 => 'admin.errors.500', 503 => 'admin.errors.503', ]; return isset($adminViews[$statusCode]) ? response()->view($adminViews[$statusCode], [], $statusCode) : null; // 不匹配时交给框架默认处理 } return $statusCode === 404 ? response()->view("customer.errors.404", [], 404) : null; }); // 处理权限拒绝异常 $this->renderable(function (AccessDeniedHttpException $exception, $request) { $view = $request->is(['admin', 'admin/*']) ? "admin.errors.403" : "customer.errors.403"; return response()->view($view, [], 403); }); } }
- 在bootstrap/app.php中注册自定义Handler
修改应用配置,指定使用自定义处理器:
return Application::configure(basePath: dirname(__DIR__)) ->withRouting( web: __DIR__.'/../routes/web.php', commands: __DIR__.'/../routes/console.php', health: '/up', ) ->withMiddleware(function (Middleware $middleware) { // 你的中间件配置 }) ->withExceptions(function (Exceptions $exceptions) { $exceptions->useHandler(\App\Exceptions\Handler::class); }) ->create();
最后一步:准备错误视图文件
确保对应的视图文件存在于指定路径:
- 后台错误视图:
resources/views/admin/errors/400.blade.php、403.blade.php、404.blade.php、500.blade.php、503.blade.php - 前台错误视图:
resources/views/customer/errors/404.blade.php(及其他需要的状态码视图)
内容的提问来源于stack exchange,提问作者Onur
相关产品推荐
相关产品推荐

