環境
步驟
建立 config\default.php 檔:
| 12
 3
 
 | 'api' => ['version' => 'v1',
 ],
 
 | 
修改 app\Providers\RouteServiceProvider.php 檔:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 
 | protected $api_version;
 
 
 public function boot()
 {
 $this->api_version = config('default.api.version');
 
 parent::boot();
 }
 
 
 
 protected function mapApiRoutes()
 {
 Route::prefix('api')
 ->middleware('api')
 ->namespace($this->namespace . '\Api\\' . ucfirst($this->api_version))
 ->group(base_path('routes/api.php'));
 }
 
 | 
在 Controllers 資料夾新增 Api\V1 資料夾,並將控制器的命名空間修改為:
| 1
 | namespace App\Http\Controllers\Api\V1;
 | 
在路由中照常使用:
| 1
 | Route::get('/', 'UserController@show');
 |