├── .styleci.yml ├── CHANGELOG.md ├── config └── config.php ├── src ├── ApiVersioning.php ├── ApiVersioningServiceProvider.php └── UriValidator.php ├── LICENSE.md ├── CONTRIBUTING.md ├── composer.json └── README.md /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | 3 | disabled: 4 | - single_class_element_per_statement 5 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `api-versioning` will be documented in this file 4 | 5 | ## 1.0.0 - 201X-XX-XX 6 | 7 | - initial release 8 | -------------------------------------------------------------------------------- /config/config.php: -------------------------------------------------------------------------------- 1 | true, 8 | 9 | 10 | /** 11 | * this prefix help that the package does not process non-api routes 12 | */ 13 | 'api_prefix' => '/api/', 14 | 15 | 16 | /** 17 | * Write your API versions in descending order 18 | * Example: ['v2.1', 'v2', 'v1'] this way version v2 is a fallback for v2.1 19 | */ 20 | 'api_versions' => [] 21 | ]; 22 | -------------------------------------------------------------------------------- /src/ApiVersioning.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 15 | $this->publishes([ 16 | __DIR__ . '/../config/config.php' => config_path('apiVersioning.php'), 17 | ], 'config'); 18 | } 19 | } 20 | 21 | /** 22 | * Register the application services. 23 | */ 24 | public function register() 25 | { 26 | // Automatically apply the package configuration 27 | $this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'apiVersioning'); 28 | 29 | new ApiVersioning(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Mahdi Bagheri 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | ## Etiquette 6 | 7 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 8 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 9 | extremely unfair for them to suffer abuse or anger for their hard work. 10 | 11 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 12 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 13 | 14 | ## Requirements 15 | 16 | If the project maintainer has any additional requirements, you will find them listed here. 17 | 18 | - **Add tests!** - We need more of them. 19 | 20 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 21 | 22 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 23 | 24 | **Happy coding**! 25 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mbpcoder/laravel-api-versioning", 3 | "description": "Enable Api Versioning for Laravel", 4 | "keywords": [ 5 | "mbpcoder", 6 | "api-versioning" 7 | ], 8 | "homepage": "https://github.com/mbpcoder/laravel-api-versioning", 9 | "license": "MIT", 10 | "type": "library", 11 | "authors": [ 12 | { 13 | "name": "Mahdi Bagheri", 14 | "email": "mahdi.bagheri@live.com", 15 | "role": "Developer" 16 | } 17 | ], 18 | "minimum-stability": "dev", 19 | "require": { 20 | "illuminate/routing": "^5|^6|^7|^8|^9|^10" 21 | }, 22 | "require-dev": { 23 | "orchestra/testbench": "^4.0|^8.0", 24 | "phpunit/phpunit": "^8.0|^10.0" 25 | }, 26 | "autoload": { 27 | "psr-4": { 28 | "MbpCoder\\ApiVersioning\\": "src" 29 | } 30 | }, 31 | "autoload-dev": { 32 | "psr-4": { 33 | "MbpCoder\\ApiVersioning\\Tests\\": "tests" 34 | } 35 | }, 36 | "scripts": { 37 | "test": "vendor/bin/phpunit", 38 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage" 39 | 40 | }, 41 | "config": { 42 | "sort-packages": true 43 | }, 44 | "extra": { 45 | "laravel": { 46 | "providers": [ 47 | "MbpCoder\\ApiVersioning\\ApiVersioningServiceProvider" 48 | ], 49 | "aliases": { 50 | "ApiVersioning": "MbpCoder\\ApiVersioning\\ApiVersioningFacade" 51 | } 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/UriValidator.php: -------------------------------------------------------------------------------- 1 | apiVersions = config('apiVersioning.api_versions'); 18 | $this->apiPrefix = config('apiVersioning.api_prefix'); 19 | $this->apiEnable = config('apiVersioning.enable'); 20 | } 21 | 22 | /** 23 | * @param Route $route 24 | * @param Request $request 25 | * @return bool|false|int 26 | * @throws \Throwable 27 | */ 28 | public function matches(Route $route, Request $request) 29 | { 30 | $result = false; 31 | 32 | 33 | $path = rtrim($request->getPathInfo(), '/') ?: '/'; 34 | 35 | /** 36 | * there is not more than one api version 37 | * or the apiVersioning is disabled 38 | * or the route is not for api 39 | * strncmp work like Illuminate\Support\Str::startsWith in laravel 40 | */ 41 | if (count($this->apiVersions) < 1 || !$this->apiEnable || strncmp($path, $this->apiPrefix, strlen($this->apiPrefix)) !== 0) { 42 | return preg_match($route->getCompiled()->getRegex(), rawurldecode($path)); 43 | } 44 | 45 | /** 46 | * basically it loop through API versions and change the path to support API fallback 47 | */ 48 | for ($index = 0; $index < count($this->apiVersions); $index++) { 49 | $result = preg_match($route->getCompiled()->getRegex(), rawurldecode($path)); 50 | 51 | /** 52 | * exit if you find a proportionate route 53 | */ 54 | if ($result) return $result; 55 | 56 | if (isset($this->apiVersions[$index + 1])) { 57 | $path = str_replace($this->apiVersions[$index], $this->apiVersions[$index + 1], $path); 58 | } 59 | } 60 | return $result; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # API Versioning for Laravel 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/mbpcoder/laravel-api-versioning.svg?style=flat-square)](https://packagist.org/packages/mbpcoder/laravel-api-versioning) 4 | [![Total Downloads](https://img.shields.io/packagist/dt/mbpcoder/laravel-api-versioning.svg?style=flat-square)](https://packagist.org/packages/mbpcoder/laravel-api-versioning) 5 | 6 | This is a very simple package to support API versioning in Laravel 8,7,6,5. this package provide fallback API capability for Laravel. 7 | 8 | ## Features 9 | * If you have one API version it will automatically disable 10 | * You can disable package in config files 11 | * You can add countless API Versions in config file 12 | * Does not affect routes except API 13 | 14 | ## Let's Code! 15 | If you call for /{version_number}/version every API have it is own route. 16 | If you call for /v2.1/hello-world it will try to call v2.1 and if it does not find a route will search v2 17 | then if the version 2 also does not have the routes it will fallback to v1 18 | ``` php 19 | 20 | // laravel route file 21 | Route::prefix('v2.1')->group(function () { 22 | Route::get('version', function () { 23 | return 'API v2.1'; 24 | }); 25 | }); 26 | 27 | Route::prefix('v2')->group(function () { 28 | Route::get('version', function () { 29 | return 'API v2'; 30 | }); 31 | }); 32 | 33 | Route::prefix('v1')->group(function () { 34 | Route::get('version', function () { 35 | return 'API v1'; 36 | }); 37 | 38 | Route::get('hello-world', function () { 39 | return 'Hello World!'; 40 | }); 41 | }); 42 | 43 | ``` 44 | 45 | ## Installation 46 | 47 | You can install the package via composer: 48 | 49 | ```bash 50 | composer require mbpcoder/laravel-api-versioning 51 | ``` 52 | 53 | ## Usage 54 | 55 | Publish config file 56 | 57 | ``` php 58 | php artisan vendor:publish --provider="MbpCoder\ApiVersioning\ApiVersioningServiceProvider" 59 | ``` 60 | 61 | ### Changelog 62 | 63 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 64 | 65 | ## Contributing 66 | 67 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 68 | 69 | ## Credits 70 | 71 | - [Mahdi Bagheri](https://github.com/mbpcoder) 72 | - [All Contributors](../../contributors) 73 | 74 | ## License 75 | 76 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 77 | 78 | ## Laravel Package Boilerplate 79 | 80 | This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com). 81 | --------------------------------------------------------------------------------