PHP code example of reindert-vetter / api-version-control
1. Go to this page and download the library: Download reindert-vetter/api-version-control library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once ('vendor/autoload.php' );
reindert-vetter / api-version-control example snippets
'releases' => [
'orders.index' => [
'<=1' => [
PrepareParameterException::class,
],
],
'orders.store|orders.update' => [
'<=2' => [
ThrowCustomException::class,
ValidateZipCode::class,
],
'<=1' => [
PrepareParameterException::class,
],
],
'default' => [
'<=1' => [
ThrowCustomException::class,
],
],
'all' => [
'<=1.0' => [
RequireUserAgent::class,
],
],
],
namespace App \VersionControl \Orders ;
use ReindertVetter \ApiVersionControl \Concerns \VersionStatement ;
class ValidateZipCode
{
use VersionStatement ;
}
if (ValidateZipCode::permitted()) {
(...)
}
namespace App \Middleware \Version ;
use Closure ;
use Illuminate \Http \Request ;
class PrepareParameterException
{
public function handle (Request $request, Closure $next)
{
namespace App \Middleware \Version ;
use Closure ;
use Illuminate \Http \Request ;
class ThrowHumanException
{
public function handle (Request $request, Closure $next)
{
$response = $next($request);
if ($response->exception) {
$response->setContent(
[
"errors" => [
[
"human" => $response->exception->getMessage(),
],
],
]
);
}
return $response;
}
}
public function index (OrderIndexRequest $request, OrderResource $resource) : ResourceCollection
{
$orders = Order::query()
->productIds($request->productIds())
->with($resource->withRelationships())
->paginate($request->limit());
return $resource::collection($orders);
}
use ReindertVetter \ApiVersionControl \Middleware \Version \Bind ;
return [
'releases' => [
'orders.index' => [
'<=1' => [
new Bind(OrderIndexRequest::class, OrderIndexRequestV1::class),
new Bind(OrderIndexResource::class, OrderIndexResourceV1::class),
],
'>=2' => [
new Bind(OrderIndexRequest::class, OrderIndexRequestV2::class),
new Bind(OrderIndexResource::class, OrderIndexResourceV2::class),
],
],
]
]
Route::middleware(['api' , ApiVersionControl::class])
->prefix('api/{version}' )
->where(['version' => 'v\d{1,3}' ])
->group(base_path('routes/api.php' ));
Route::middleware(['api' , ApiVersionControl::class])
->prefix('api' )
->as('no_version.' )
->group(base_path('routes/api.php' ));
Route::middleware(['api' , ApiVersionControl::class])
->prefix('api/{version}' )
->where(['version' => 'v\d{1,3}' ])
->group(base_path('routes/api.php' ));