路由
- 中文文档:https://learnku.com/docs/laravel/12.x/routing/16944
- 大部分文档在官方可查,这里只记录一些常用的内容。
自定义路由文件
- 常规路由文件:
routes/routes/web.php - API 路由文件:
routes/routes/api.php
php
# 1. 创建 `routes/admin.php`
# 2. 在 `bootstrap/app.php` 加载路由文件
<?php
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Support\Facades\Route;
$app = Application::configure(basePath: dirname(__DIR__))
->withRouting(
api: __DIR__ . '/../routes/api.php',
commands: __DIR__ . '/../routes/console.php',
health: '/up',
then: function (): void {
// 自定义路由
Route::prefix('admin')->group(base_path('routes/admin.php'));
}
)
->withMiddleware(function (Middleware $middleware): void {
// 注册全局中间件
})
->withExceptions(function (Exceptions $exceptions): void {
$exceptions->render(function (Throwable $e) {
// 自定义异常处理
});
})->create();
return $app;