基本路由
1 2 3
| Route::get('foo', function () { return 'Hello World'; });
|
默认路由文件
@
后的index
为控制器中的方法名。
1
| Route::get('/user', 'UserController@index');
|
可用路由方法
1 2 3 4 5 6
| Route::get($uri, $callback); Route::post($uri, $callback); Route::put($uri, $callback); Route::patch($uri, $callback); Route::delete($uri, $callback); Route::options($uri, $callback);
|
响应多个 HTTP 请求的路由
1 2 3 4 5 6 7
| Route::match(['get', 'post'], '/', function () {
});
Route::any('/', function () {
});
|
CSRF 保护
指向 web 路由文件中定义的 POST、PUT 或 DELETE 路由的任何 HTML 表单都应该包含一个 CSRF 令牌字段,否则,这个请求将会被拒绝。
1 2 3 4
| <form method="POST" action="/user"> @csrf ... </form>
|
重定向路由
快速的实现重定向
1 2 3
| Route::redirect('/here', '/there'); Route::redirect('/here', '/there', 301); Route::permanentRedirect('/here', '/there');
|
视图路由
返回一个视图
1 2 3
| Route::view('/index', 'welcome');
Route::view('/index', 'welcome', ['data' => $data]);
|
参数
必填参数:
1 2 3 4 5 6 7 8
| Route::get('user/{id}', function ($id) { return 'User '.$id; });
Route::get('user/{id}/{post_id}', function ($id, $post_id) { });
|
可选参数:
1 2 3 4 5 6 7
| Route::get('user/{name?}', function ($name = null) { return $name; });
Route::get('user/{name?}', function ($name = 'Tom') { return $name; });
|
正则表达式约束
使用路由实例上的 where 方法约束路由参数的格式:
1 2 3 4 5 6 7 8 9 10 11
| 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]+']);
|
全局约束
希望某个具体的路由参数都遵循同一个正则表达式的约束,就使用 pattern 方法在 RouteServiceProvider 的 boot 方法中定义这些模式,定义好之后,便会自动应用这些规则到所有使用该参数名称的路由上:
1 2 3 4 5 6 7 8 9 10 11
|
public function boot() { Route::pattern('id', '[0-9]+');
parent::boot(); }
|
路由命名
1 2 3 4 5
| Route::get('user/info', function () { })->name('info');
Route::get('user/info', 'UserInfoController@show')->name('info');
|
生成指定路由的 URL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| $url = route('info');
return redirect()->route('info');
Route::get('user/{id}/info', function ($id) { })->name('info'); $url = route('info', ['id' => 1]);
Route::get('user/{id}/info', function ($id) { })->name('info'); $url = route('info', ['id' => 1, 'images' => 'yes']);
|
中间件
1 2 3 4 5 6 7 8 9
| Route::middleware(['Auth', 'PassUth'])->group(function () { Route::get('/', function () { });
Route::get('user/info', function () { }); });
|
路由前缀
1 2 3 4 5 6 7 8 9 10 11
| Route::prefix('admin')->group(function () { Route::get('users', function () { }); });
Route::name('admin.')->group(function () { Route::get('users', function () { })->name('users'); });
|
路由模型绑定
1 2 3
| Route::get('api/users/{user}', function (App\User $user) { return $user->email; });
|
回退路由
1 2 3 4
| Route::fallback(function () { });
|
限流
1 2 3 4 5
| Route::middleware('auth:api', 'throttle:60,1')->group(function () { Route::get('/user', function () { }); });
|
动态限流
1 2 3 4 5
| Route::middleware('auth:api', 'throttle:rate_limit,1')->group(function () { Route::get('/user', function () { }); });
|
表单方法伪造
1 2 3 4 5 6 7 8 9 10
| <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>
|
访问当前路由
1 2 3 4 5
| $route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
|
待更新…