限时免费试用:欢迎注册 api.bigmodel.org ,快速体验大模型 API 接入服务。
当前位置:首页 >开发者 >网站框架 >后端框架 >Laravel

laravel5.4 | 路由

分类:Laravel时间:2019-02-08浏览:2827
## 基础路由 Route::get('foo', function () { return 'Hello World'; }); routes/web.php 文件定义了web界面的路由,这些路由被分配了web中间件组,从而可以提供session和csrf防护等功能。routes/api.php 中的路由是无状态的,被分配了 api 中间件组。 #### 路由的形式 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防护 请求方式为PUT、POST或DELETE的HTML表单都会包含一个CSRF令牌字段,否则,请求会被拒绝.可以参考[CSRF文档](https://laravel.com/docs/5.4/csrf): ## 路由参数 #### 必选参数 参数不能含有- ,可使用_ 单参数 Route::get('user/{id}', function ($id) { return 'User '.$id; }); 多参数 Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) { // }); #### 可选参数 在参数名后加一个 ? Route::get('user/{name?}', function ($name = 'John') { 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 方法中定义约束模式: /** * 定义路由模型绑定,模式过滤器等 * * @param \Illuminate\Routing\Router $router * @return void * @translator http://laravelacademy.org */ public function boot() { Route::pattern('id', '[0-9]+'); parent::boot(); } 一旦模式被定义,将会自动应用到所有包含该参数名的路由中: Route::get('user/{id}', function ($id) { // 只有当 {id} 是数字时才会被调用 }); ## 命名路由 name Route::get('user/profile', function () {})->name('profile'); Route::get('user/profile', 'UserController@showProfile')->name('profile'); #### 为命名路由生成URL route 为给定路由分配名称之后,就可以通过辅助函数 route 为该命名路由生成 URL: $url = route('profile'); return redirect()->route('profile'); 如果命名路由定义了参数,可以将该参数作为第二个参数传递给 route 函数。给定的路由参数将会自动插入到 URL 中: Route::get('user/{id}/profile', function ($id) { // })->name('profile'); $url = route('profile', ['id' => 1]); ## 群组路由 在多个路由中共享路由属性,比如中间件和命名空间等Route::group #### 中间件middleware Route::group(['middleware' => 'auth'], function () { Route::get('/', function () { // 使用 Auth 中间件 }); }); #### 命名空间namespace Route::group(['namespace' => 'Admin'], function(){ // 控制器在 "App\Http\Controllers\Admin" 命名空间下 }); #### 子域名路由domain Route::group(['domain' => '{account}.myapp.com'], function () { Route::get('user/{id}', function ($account, $id) { // }); }); #### 路由前缀prefix Route::group(['prefix' => 'admin'], function () { Route::get('users', function () { // 匹配 "/admin/users" URL }); }); ## 路由模型绑定 #### 隐式绑定 Route::get('api/users/{user}', function (App\User $user) { return $user->email; }); 在这个例子中,由于类型声明了 Eloquent 模型 App\User,对应的变量名 $user 会匹配路由片段中的{user},这样,Laravel 会自动注入与请求 URI 中传入的 ID 对应的用户模型实例。 如果数据库中找不到对应的模型实例,会自动生成 HTTP 404 响应。 #### 自定义键名 如果你想要在隐式模型绑定中使用数据表的其它字段,可以重写 Eloquent 模型类的 getRouteKeyName 方法: /** * Get the route key for the model. * * @return string */ public function getRouteKeyName() { return 'slug'; } #### 显式绑定 要注册显式绑定,需要使用路由的 model 方法来为给定参数指定绑定类。应该在 RouteServiceProvider 类的 boot 方法中定义模型绑定: public function boot() { parent::boot(); Route::model('user', App\User::class); } 接下来,定义一个包含 {user} 参数的路由: $router->get('profile/{user}', function(App\User $user) { // }); 由于我们已经绑定 {user} 参数到 App\User 模型,User 实例会被注入到该路由。因此,如果请求 URL 是 profile/1,就会注入一个用户 ID 为 1 的 User 实例。 如果匹配的模型实例在数据库不存在,会自动生成并返回 HTTP 404 响应。 #### 自定义解析逻辑 如果你想要使用自定义的解析逻辑,需要使用 Route::bind 方法,传递到 bind 方法的闭包会获取到 URI 请求参数中的值,并且返回你想要在该路由中注入的类实例: public function boot() { parent::boot(); Route::bind('user', function($value) { return App\User::where('name', $value)->first(); }); } ## 表单方法伪造 当定义 PUT、PATCH 或 DELETE 路由时,需要添加一个隐藏的 _method 字段到表单中 或使用辅助函数 method_field {{ method_field('PUT') }} ## 访问当前路由 $route = Route::current(); $name = Route::currentRouteName(); $action = Route::currentRouteAction();
本站文章如未注明出处均为原创,转载请注明出处,如有侵权请邮件联系站长。
0/500
Share your thoughts respectfully.