Add versioning to Laravel routes

This commit is contained in:
Bubka 2021-11-04 23:57:23 +01:00
parent 9ca9fcc9ef
commit 0383f07ad7
3 changed files with 35 additions and 10 deletions

View File

@ -41,7 +41,7 @@ class Kernel extends HttpKernel
\App\Http\Middleware\CustomCreateFreshApiToken::class,
],
'api' => [
'api.v1' => [
'throttle:60,1',
'bindings',
\App\Http\Middleware\LogoutInactiveUser::class,

View File

@ -35,11 +35,11 @@ public function boot()
*/
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
//
$this->mapApiVersionOneRoutes();
// $this->mapApiVersionTwoRoutes();
}
/**
@ -57,17 +57,42 @@ protected function mapWebRoutes()
}
/**
* Define the "api" routes for the application.
* Define the "v1 api" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
protected function mapApiRoutes()
protected function mapApiVersionOneRoutes()
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
Route::prefix('api/v1')
->middleware('api.v1')
->namespace($this->getApiNamespace(1))
->group(base_path('routes/api/v1.php'));
}
/**
* Define the "v2 api" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
// protected function mapApiVersionTwoRoutes()
// {
// Route::prefix('api/v2')
// ->middleware('api.v2')
// ->namespace($this->getApiNamespace(2))
// ->group(base_path('routes/api/v2.php'));
// }
/**
* Build Api namespace based on provided version
*
* @return string The Api namespace
*/
private function getApiNamespace($version)
{
return 'App\Api\v' . $version . '\Controllers';
}
}