PHP code example of uttamrabadiya / api-version-manager
1. Go to this page and download the library: Download uttamrabadiya/api-version-manager 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');
/* Start to develop here. Best regards https://php-download.com/ */
uttamrabadiya / api-version-manager example snippets
Route::prefix('v1')->group(function () {
Route::get('endpoint1', [SomeController::class, 'endpoint1']); // Available on v1 & v2 (Via default fallback)
Route::get('endpoint2', [SomeController::class, 'endpoint2']); // Available on v1 & v2 (Via default fallback)
});
Route::prefix('v2')->group(function () {
Route::get('new-endpoint', [SomeController::class, 'endpoint3']); // Available only on v2
});
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use App\Http\Resources\Versioned\EndpointResource; // Mandatory to use versioned resource only. Don't use `App\Http\Resources\V1\EndpointResource` or `App\Http\Resources\V2\EndpointResource`
use App\Http\Requests\Versioned\NewEndpointRequest; // Mandatory to use versioned request only. Don't use `App\Http\Requests\V1\NewEndpointRequest` or `App\Http\Requests\V2\NewEndpointRequest`
use Illuminate\Http\Request;
class SomeController extends Controller
{
public function endpoint1(Request $request)
{
return DashboardResource::item(['some' => 'data']); // Replacement of native `new DashboardResource(['some' => 'data'])` resource
}
public function endpoint1(Request $request)
{
return DashboardResource::collection(['some' => 'data']);
}
public function endpoint3(NewEndpointRequest $request)
{
return DashboardResource::item(['some' => 'data']);
}
}