Giter Club home page Giter Club logo

api-version-manager's Introduction

Laravel API Version Manager

License: MIT Latest Version on Packagist GitHub Tests Action Status Total Downloads

The Laravel API Version Manager Package streamlines the management of API endpoint versions in Laravel applications. This package empowers you to effortlessly handle API versioning, eliminating the necessity to create individual controllers for each version. Its design presents a flexible and efficient solution, enabling you to define fallback versions and effortlessly generate version-specific Requests and Resources.

Features

  • Fallback Versions: Define fallback versions for your API routes, ensuring smooth transitions and maintaining backward compatibility.
  • Single Controller: Eliminate the need to create separate controllers for each API version. Our package dynamically injects version-specific Request and Resource classes into your existing controller.
  • Effortless Versioning: Easily manage API versions through route configuration. The package automatically handles the resolution of version-specific components, streamlining your development process.

Installation

You can install the package via Composer:

composer require uttamrabadiya/api-version-manager

Configuration

Publish Config File

It is mandatory to publish the config file before using the package. You can publish the config file using the following command:

php artisan vendor:publish --tag=api-version-manager

Mandatory Configurations:

  • Define available versions array in config
  • Define default version version in config

Other possible configurations are:

  • app_http_namespace (default: App\Http) - Generally we use App\Http namespace to store all our requests & resources classes, but if you are using different namespace then you can define it here.
  • api_prefix (default: api) - API prefix for all versioned routes.
  • use_fallback_entity (default: true) - If you want to use fallback entity for all request & resource class then set this to true, otherwise set it to false. For example, you define SampleRequest in V1, and now you want to use same request in V2 then you can set this option to true and it will automatically use SampleRequest from V1.

Available Commands

Create a new versioned request

php artisan make:versioned-request {name}

Possible options:

  • --force: Overwrite the request if it already exists.

Create a new versioned resource

php artisan make:versioned-resource {name}

Possible options:

  • --collection: Create a resource collection instead of a single resource.
  • --force: Overwrite the resource if it already exists.

Usage

Example of api.php file:

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 
});

Example of SomeController.php file:

<?php

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']);
    }
}

Changelog

Please see CHANGELOG for more information what has changed recently.

License

The MIT License (MIT). Please see License File for more information.

api-version-manager's People

Contributors

uttamrabadiya avatar

Stargazers

Sebastian Hädrich avatar fateme maddahi avatar Devon Mather avatar Rich Perez avatar Daniel Ignacio Fernández avatar Ever Daniel Barreto avatar moneya avatar Kevin Jiang avatar  avatar  avatar Ahmed Elshafie avatar Nirali avatar Divyank avatar

Watchers

Divyank avatar  avatar Nirali avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.