文章目录

你可以在 app/Http/routes.php 文件中定义应用程序的大多数路由,该文件将会被 App\Providers\RouteServiceProvider 类加载。最基本的 Laravel 路由仅接受 URI 和一个闭包:
1 2 3 4 5 6 |
路由传参
必选参数
有时候你可能需要从 URI 中获取一些参数。例如,从 URL 获取用户的 ID。这时可通过自定义路由参数来获取:
1 2 3 4 |
Route::get('user/{id}', function ($id) { return 'User '.$id; }); |
你可以依照路由需要,定义任意数量的路由参数:
1 2 3 4 |
Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) { // }); |
路由的参数都会被放在「大括号」内。当运行路由时,参数会通过路由闭包来传递。
注意: 路由参数不能包含 - 字符。请用下划线 (_) 替换。
可选的路由参数
有时候你需要指定可选的路由参数,可以在参数名称后面加上 ? 来实现:
1 2 3 4 5 6 7 8 |
Route::get('user/{name?}', function ($name = null) { return $name; }); Route::get('user/{name?}', function ($name = 'John') { return $name; }); |
可选的路由参数
有时候你需要指定可选的路由参数,可以在参数名称后面加上 ? 来实现:
1 2 3 4 5 6 7 8 |
Route::get('user/{name?}', function ($name = null) { return $name; }); Route::get('user/{name?}', function ($name = 'John') { return $name; }); |
正则表达式限制参数
你可以使用 where 方法来限制路由参数格式。where 方法接受参数的名称和定义参数应该如何被限制的正则表达式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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 12 13 |
/** * 定义你的路由模型绑定,模式过滤器等。 * * @param \Illuminate\Routing\Router $router * @return void */ public function boot(Router $router) { $router->pattern('id', '[0-9]+'); parent::boot($router); } |
模式一旦被定义,便会自动应用到所有使用该参数名称的路由上:
1 2 3 4 |
Route::get('user/{id}', function ($id) { // Only called if {id} is numeric. }); |
laravel 命名路由:
命名路由让你可以更方便的为特定路由生成 URL 或进行重定向。你可以使用 as 数组键指定名称到路由上:
1 2 3 4 |
Route::get('user/profile', ['as' => 'profile', function () { // }]); |
还可以指定路由名称到控制器动作:
1 2 3 4 |
Route::get('user/profile', [ 'as' => 'profile', 'uses' => 'UserController@showProfile' ]); |
除了可以在路由的数组定义中指定路由名称外,你也可以在路由定义后方链式调用 name 方法:
1 2 |
Route::get('user/profile', 'UserController@showProfile')->name('profile'); |
路由群组:
路由群组允许你共用路由属性,例如:中间件、命名空间,你可以利用路由群组统一为多个路由设置共同属性,而不需在每个路由上都设置一次。共用属性被指定为数组格式,当作 Route::group 方法的第一个参数。
如果你使用了 路由群组,那么你可以在路由群组的属性数组中指定一个 as 关键字,这将允许你为路由群组中的所有路由设置相同的前缀名称:
1 2 3 4 5 6 |
Route::group(['as' => 'admin::'], function () { Route::get('dashboard', ['as' => 'dashboard', function () { // 路由名称为「admin::dashboard」 }]); }); |
对命名路由生成 URLs
一旦你在指定的路由中分配了名称,则可通过 route 函数来使用路由名称生成 URLs 或重定向:
1 2 3 4 |
$url = route('profile'); $redirect = redirect()->route('profile'); |
如果路由定义了参数,那么你可以把参数作为第二个参数传递给 route 方法。指定的参数将自动加入到 URL 中:
1 2 3 4 5 6 |
Route::get('user/{id}/profile', ['as' => 'profile', function ($id) { // }]); $url = route('profile', ['id' => 1]); |
路由群组#
路由群组允许你共用路由属性,例如:中间件、命名空间,你可以利用路由群组统一为多个路由设置共同属性,而不需在每个路由上都设置一次。共用属性被指定为数组格式,当作 Route::group 方法的第一个参数。
为了了解更多路由群组的相关内容,我们可通过几个常用样例来熟悉这些特性。
中间件#
指定中间件到所有群组内的路由中,则可以在群组属性数组里使用 middleware 参数。中间件将会依照列表内指定的顺序运行:
1 2 3 4 5 6 7 8 9 10 |
Route::group(['middleware' => 'auth'], function () { Route::get('/', function () { // 使用 Auth 中间件 }); Route::get('user/profile', function () { // 使用 Auth 中间件 }); }); |
命名空间
另一个常见的例子是,指定相同的 PHP 命名空间给控制器群组。可以使用 namespace 参数来指定群组内所有控制器的命名空间:
1 2 3 4 5 6 7 8 9 10 |
Route::group(['namespace' => 'Admin'], function() { // 控制器在「App\Http\Controllers\Admin」命名空间 Route::group(['namespace' => 'User'], function() { // 控制器在「App\Http\Controllers\Admin\User」命名空间 }); }); |
请记住,默认 RouteServiceProvider 会在命名空间群组内导入你的 routes.php 文件,让你不用指定完整的 App\Http\Controllers 命名空间前缀就能注册控制器路由。所以,我们只需要指定在基底 App\Http\Controllers 根命名空间之后的部分命名空间。
子域名路由
路由群组也可以被用来做处理通配符的子域名。子域名可以像路由 URIs 分配路由参数,让你在路由或控制器中获取子域名参数。使用路由群组属性数组上的 domain 指定子域名变量名称:
1 2 3 4 5 6 |
Route::group(['domain' => '{account}.myapp.com'], function () { Route::get('user/{id}', function ($account, $id) { // }); }); |
路由前缀
通过路由群组数组属性中的 prefix,在路由群组内为每个路由指定的 URI 加上前缀。例如,你可能想要在路由群组中将所有的路由 URIs 加上前缀 admin:
1 2 3 4 5 6 |
Route::group(['prefix' => 'admin'], function () { Route::get('users', function () { // 符合「/admin/users」URL }); }); |
你也可以使用 prefix 参数去指定路由群组中共用的参数:
1 2 3 4 5 6 |
Route::group(['prefix' => 'accounts/{account_id}'], function () { Route::get('detail', function ($account_id) { // 符合 accounts/{account_id}/detail URL }); }); |
