基本路由
Route::get('foo', function () {
return 'Hello World';
});
默认路由文件
@
后的index
为控制器中的方法名。
Route::get('/user', 'UserController@index');
可用路由方法
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 请求的路由
Route::match(['get', 'post'], '/', function () {
});
Route::any('/', function () {
});
CSRF 保护
指向 web 路由文件中定义的 POST、PUT 或 DELETE 路由的任何 HTML 表单都应该包含一个 CSRF 令牌字段,否则,这个请求将会被拒绝。
<form method="POST" action="/user">
@csrf
...
</form>
重定向路由
快速的实现重定向
Route::redirect('/here', '/there');
Route::redirect('/here', '/there', 301);
Route::permanentRedirect('/here', '/there');
视图路由
返回一个视图
Route::view('/index', 'welcome');
//通过数组传递一组数据给视图,作为可选的第三个参数
Route::view('/index', 'welcome', ['data' => $data]);
参数
必填参数:
//从 URL 中捕获用户的 ID
Route::get('user/{id}', function ($id) {
return 'User '.$id;
});
//定义多个参数
Route::get('user/{id}/{post_id}', function ($id, $post_id) {
//
});
可选参数:
Route::get('user/{name?}', function ($name = null) {
return $name;
});
Route::get('user/{name?}', function ($name = 'Tom') {
return $name;
});
正则表达式约束
使用路由实例上的 where 方法约束路由参数的格式:
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 方法中定义这些模式,定义好之后,便会自动应用这些规则到所有使用该参数名称的路由上:
/**
* 定义你的路由模型绑定, pattern 过滤器等
*
* @return void
*/
public function boot()
{
Route::pattern('id', '[0-9]+');
parent::boot();
}
路由命名
Route::get('user/info', function () {
//
})->name('info');
Route::get('user/info', 'UserInfoController@show')->name('info');
生成指定路由的 URL
// 生成 URL...
$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']);
// /user/1/info?images=yes
中间件
Route::middleware(['Auth', 'PassUth'])->group(function () {
Route::get('/', function () {
// 使用 Auth 和 PassUth 中间件
});
Route::get('user/info', function () {
// 使用 Auth 和 PassUth 中间件
});
});
路由前缀
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');
});
路由模型绑定
Route::get('api/users/{user}', function (App\User $user) {
return $user->email;
});
回退路由
//回退路由应始终是您应用程序注册的最后一个路由
Route::fallback(function () {
//
});
限流
Route::middleware('auth:api', 'throttle:60,1')->group(function () {
Route::get('/user', function () {
//指定一个经过身份验证并且用户每分钟访问频率不超过 60 次的路由组
});
});
动态限流
Route::middleware('auth:api', 'throttle:rate_limit,1')->group(function () {
Route::get('/user', function () {
//将属性名称传递给 throttle 中间件,以便它用于计算最大请求数
});
});
表单方法伪造
<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 = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
待更新…