Laravel 路由 route

HTTP方法

1
2
3
4
5
6
Route::get($url, $callback);
Route::post($url, $callback);
Route::put($url, $callback);
Route::patch($url, $callback);
Route::delete($url, $callback);
Route::options($url, $callback);

路由参数

1
2
3
Route::get('user/{id}', function ($id) {
return 'User '.$id;
};

路由分组

1
2
3
4
5
Route::group(['prefix' => 'admin'], function () {
Route::get('users', function () {
// 匹配包含 "/admin/users" 的 URL
});
});

绑定模型

1
2
3
Route::get('api/users/{user}', function (App\User $user) {
return $user->email;
});

Resource 格式

1
Route::resource('photos', 'PhotoController');
动作 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