Created
September 12, 2017 09:18
-
-
Save pvledoux/a76f599e6519f0f87dc8dd185d29d5dc to your computer and use it in GitHub Desktop.
Laravel 5.5 API versioning based header. It's not working because the route has already been binded, but I kept it here for the record
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App; | |
/** | |
* ApiVersion class | |
* | |
* @author pvl | |
*/ | |
class ApiVersion | |
{ | |
private static $valid_api_versions = [ | |
1 => 'v1', | |
2 => 'v2', | |
]; | |
/** | |
* Resolve the requested api version. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return integer | |
*/ | |
public static function get($request) { | |
return (int) $request->header('api-version'); | |
} | |
/** | |
* Determines if a version is valid or not | |
* | |
* @param integer $apiVersion | |
* @return bool | |
*/ | |
public static function isValid($apiVersion) { | |
return array_key_exists($apiVersion, self::$valid_api_versions); | |
} | |
/** | |
* Resolve namespace for a api version | |
* | |
* @param integer $apiVersion | |
* @return string | |
*/ | |
public static function getNamespace($apiVersion) | |
{ | |
if (!self::isValid($apiVersion)) { | |
return null; | |
} | |
return self::$valid_api_versions[$apiVersion]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Http\Middleware; | |
use App\ApiVersion; | |
use Closure; | |
use Illuminate\Http\Request; | |
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; | |
/** | |
* ApiVersionMiddleware class | |
* | |
* @author pvl | |
*/ | |
class ApiVersionMiddleware | |
{ | |
public function handle(Request $request, Closure $next) | |
{ | |
$route = $request->route(); | |
$actions = $route->getAction(); | |
$requestedApiVersion = ApiVersion::get($request); | |
if (!ApiVersion::isValid($requestedApiVersion)) { | |
throw new BadRequestHttpException('No valid api version given'); | |
} | |
$apiNamespace = ApiVersion::getNamespace($requestedApiVersion); | |
$actions['uses'] = str_replace('v1', $apiNamespace, $actions['uses']); | |
$actions['namespace'] .= "\\" . $apiNamespace; | |
$actions['controller'] = str_replace('v1', $apiNamespace, $actions['controller']); | |
$route->setAction($actions); | |
return $next($request); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http; | |
use Illuminate\Foundation\Http\Kernel as HttpKernel; | |
class Kernel extends HttpKernel | |
{ | |
/** | |
* The application's global HTTP middleware stack. | |
* | |
* These middleware are run during every request to your application. | |
* | |
* @var array | |
*/ | |
protected $middleware = [ | |
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, | |
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, | |
\App\Http\Middleware\TrimStrings::class, | |
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, | |
\App\Http\Middleware\TrustProxies::class, | |
]; | |
/** | |
* The application's route middleware groups. | |
* | |
* @var array | |
*/ | |
protected $middlewareGroups = [ | |
'web' => [ | |
\App\Http\Middleware\EncryptCookies::class, | |
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, | |
\Illuminate\Session\Middleware\StartSession::class, | |
// \Illuminate\Session\Middleware\AuthenticateSession::class, | |
\Illuminate\View\Middleware\ShareErrorsFromSession::class, | |
\App\Http\Middleware\VerifyCsrfToken::class, | |
\Illuminate\Routing\Middleware\SubstituteBindings::class, | |
], | |
'api' => [ | |
'api-version', | |
'throttle:60,1', | |
'bindings', | |
], | |
]; | |
/** | |
* The application's route middleware. | |
* | |
* These middleware may be assigned to groups or used individually. | |
* | |
* @var array | |
*/ | |
protected $routeMiddleware = [ | |
'auth' => \Illuminate\Auth\Middleware\Authenticate::class, | |
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, | |
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, | |
'can' => \Illuminate\Auth\Middleware\Authorize::class, | |
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, | |
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, | |
'api-version' => \App\Http\Middleware\ApiVersionMiddleware::class, | |
]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Http\Request; | |
/* | |
|-------------------------------------------------------------------------- | |
| API Routes | |
|-------------------------------------------------------------------------- | |
| | |
| Here is where you can register API routes for your application. These | |
| routes are loaded by the RouteServiceProvider within a group which | |
| is assigned the "api" middleware group. Enjoy building your API! | |
| | |
*/ | |
Route::middleware('api')->get('/user', 'v1\UserController@index'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment