企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] # 路由 >[success] routes/web.php 默认分配 web 中间件组 > routes/api.php 默认分配 api 中间件组,路由默认添加`api`前缀 > 参看设置文件 app\Providers\RouteServiceProvider.php ## 基本路由 ``` // 路由方法 Route::get($uri, $callback); Route::post($uri, $callback); Route::put($uri, $callback); Route::patch($uri, $callback); Route::delete($uri, $callback); Route::options($uri, $callback); // 一个或多个路由方法 Route::match(['get', 'post'], '/', function () { // }); // 所有路由方法 Route::any('foo', function () { // }); // CSRF 保护,web 路由(POST、PUT、DELETE) <form method="POST" action="/profile"> @csrf ... </form> // 重定向路由,,默认会返回状态码 302 Route::redirect('/here', '/there'); // 301 Route::redirect('/here', '/there', 301); Route::permanentRedirect('/here', '/there'); // 视图路由 Route::view('/welcome', 'welcome'); Route::view('/welcome', 'welcome', ['name' => 'Taylor']); // 控制器路由,只需要指定命名空间为`App\Http\Controllers`之后的部分 Route::get('user/{id}', 'UserController@show'); // 完整的控制器类名为 App\Http\Controllers\Photos\AdminController Route::get('foo', 'Photos\AdminController@method'); ``` ## 路由参数 ``` // 必填参数 Route::get('user/{id}', function ($id) { return 'User '.$id; }); Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) { // }); // 可选参数 Route::get('user/{name?}', function ($name = null) { return $name; }); Route::get('user/{name?}', function ($name = 'John') { return $name; }); // 正则表达式约束 Route::get('user/{name}', function ($name) { // })->where('name', '[A-Za-z]+'); Route::get('user/{id}', function ($id) { // })->where('id', '[0-9]+'); Route::get('user/{id}/{name}', function ($id, $name) { // })->where(['id' => '[0-9]+', 'name' => '[a-z]+']); // 全局约束,在 RouteServiceProvider 的 boot 方法中定义 public function boot() { Route::pattern('id', '[0-9]+'); parent::boot(); } // 编码正斜杠字符 // 必须使用 where 条件正则表达式显式地允许 / 成为占位符的一部分 // 注意:仅支持在最后一个参数生效 Route::get('search/{search}', function ($search) { return $search; })->where('search', '.*'); ``` ## 路由命名 ``` Route::get('user/profile', function () { // })->name('profile'); Route::get('user/profile', 'UserProfileController@show')->name('profile'); // 生成指定路由的 URL $url = route('profile'); return redirect()->route('profile'); // 参数设置 Route::get('user/{id}/profile', function ($id) { // })->name('profile'); $url = route('profile', ['id' => 1]); // 检查当前请求是否指向了某个路由 public function handle($request, Closure $next) { if ($request->route()->named('profile')) { // } return $next($request); } ``` ## 路由组 ``` // 中间件 Route::middleware(['first', 'second'])->group(function () { Route::get('/', function () { // // 使用 first 和 second 中间件 }); Route::get('user/profile', function () { // // 使用 first 和 second 中间件 }); }); // 命名空间 Route::namespace('Admin')->group(function () { // 在 "App\Http\Controllers\Admin" 命名空间下的控制器 Route::get('/manage', 'IndexController@index'); }); // 子域名路由 Route::domain('{account}.myapp.com')->group(function () { Route::get('user/{id}', function ($account, $id) { // }); }); // 路由前缀 Route::prefix('admin')->group(function () { Route::get('users', function () { // 匹配包含 "/admin/users" 的 URL }); }); // 路由名称前缀 Route::name('admin.')->group(function () { Route::get('users', function () { // 指定路由名为 "admin.users"... })->name('users'); }); ``` ## 路由模型绑定 ``` // 隐式绑定,在数据库中找不到对应的模型实例,将会自动生成 404 异常 Route::get('api/users/{user}', function (App\User $user) { return $user->email; }); // 自定义路由参数的键名,在 Eloquent 模型上重写 getRouteKeyName 方法 public function getRouteKeyName() { return 'slug'; } // 显式绑定,在 RouteServiceProvider 类中的 boot 方法内定义这些显式模型绑定 public function boot() { parent::boot(); Route::model('user', App\User::class); } Route::get('profile/{user}', function ($user) { return $user->email; }); // 自定义逻辑解析 public function boot() { parent::boot(); Route::bind('user', function ($value) { return App\User::where('name', $value)->first() ?? abort(404); }); } // 或者重写 Eloquent 模型上的 resolveRouteBinding 方法 public function resolveRouteBinding($value) { return $this->where('name', $value)->first() ?? abort(404); } ``` ## 回退路由 >[danger] 注意:回退路由应始终是你应用程序注册的最后一个路由。 ``` // 执行未匹配的路由 Route::fallback(function () { abort(404); }); ``` ## 访问控制 ``` // 经过身份验证并且用户每分钟访问频率不超过 60 次的路由组 Route::middleware('auth:api', 'throttle:60,1')->group(function () { Route::get('/user', function () { // }); }); // 动态访问控制 // 根据已验证的 User 模型的属性 rate_limit,指定动态请求的最大值。 Route::middleware('auth:api', 'throttle:rate_limit,1')->group(function () { Route::get('/user', function () { // }); }); ``` >[success] 使用中间件 auth:api 需要传递令牌参数 ## 表单方法伪造 ``` // PUT、PATCH、DELETE <form action="/foo/bar" method="POST"> <input type="hidden" name="_method" value="PUT"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> </form> <form action="/foo/bar" method="POST"> @method('PUT') @csrf </form> ``` ## 访问当前路由 ``` // 使用 Route facade 上的方法访问处理传入请求的路由的信息 $route = Route::current(); $name = Route::currentRouteName(); $action = Route::currentRouteAction(); ``` ## 资源路由 ``` // 生成API资源路由 $ php artisan make:controller PhotoController --resource Route::resource('photos', 'PhotoController'); // 定义多个资源控制器 Route::resources([ 'photos' => 'PhotoController', 'posts' => 'PostController' ]); ``` **资源路由请求方法** HTTP 方法 | URI | 动作 | 路由名称 ----------|-----------------------|--------------|--------------------- GET | `/photos` | index | photos.index GET | `/photos/create` | create | photos.create POST | `/photos` | store | photos.store GET | `/photos/{photo}` | show | photos.show GET | `/photos/{photo}/edit` | edit | photos.edit PUT/PATCH | `/photos/{photo}` | update | photos.update DELETE | `/photos/{photo}` | destroy | photos.destroy ## API资源路由 >[success] 排除 create 和 edit 显示 HTML 模板的路由 ``` // 生成API资源路由 $ php artisan make:controller Api/PhotoController --api Route::apiResource('photos', 'Api\PhotoController'); Route::apiResources([ 'photos' => 'PhotoController', 'posts' => 'PostController' ]); ``` ## 路由缓存 >[danger] 注意:基于闭包的路由无法被缓存。要使用路由缓存,需要将闭包路由转换成控制器路由。 ``` // 生成 $ php artisan route:cache // 清除 $ php artisan route:clear ```