Download the PHP package adelynx/laravel-api-resources without Composer
On this page you can find all versions of the php package adelynx/laravel-api-resources. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download adelynx/laravel-api-resources
More information about adelynx/laravel-api-resources
Files in adelynx/laravel-api-resources
Package laravel-api-resources
Short Description Manage your resources maintaining API versioning
License MIT
Homepage https://github.com/adelynx/laravel-api-resources
Informations about the package laravel-api-resources
Laravel API Resources
Manage your resources maintaining API versioning. With a simple middleware separate routes by API version, and smart instanciate Http\Resources based on this version.
Add the middleware 'api.v:2'
on your api/v2 group.
And then api_resource('App\User')->make($user)
is the same as new App\Http\Resources\App\v2\User($user)
, but version free.
Installation
You can install this package via composer using:
The package will automatically register itself.
Config
To publish the config file to config/api.php
run:
This will publish a file api.php
in your config directory with the following content:
Middleware
Install this middleware on your Http/Kernel.php
under the $routeMiddleware
Configure correctly
For this package to work, you need to understand how it requires resources.
If we have the following config:
This means that if you include the Api\User
resource, it will instantiate App\Http\Resources\Api\v2\User
.
Api
works for sub organizing your structure, but you can put your Resources versioned folders in the root, like this:
Now if we include User
, it will instantiate App\Http\Resources\v2\User
.
Fallback
When you use a version that is NOT the latest, if you try to include a Resource that's NOT defined inside that version's directory, this will automatically fallback in the LATEST version.
This way you don't have to duplicate new resources on previous versions.
Usage
Middleware
When you group your API routes, you should now apply the middleware api.v
into the group like this:
That way, if you use the Facade, you can check the current version by doing APIResource::getVersion()
and will return the version specified on the middleware.
Facade
There are many ways to create resources. You can use the Facade accessor:
Global helper
Collections
Instead of make
, use collection
for arrays, just like Laravel's documentation.
Nested resources
To take advantage of the fallback functionality, it's recommended to use api_resource
inside the resources. This way you preserve the right version, or the latest if it's not defined.
Multiple APIs
There might be the case where you have more than one API living on the same project, but using different versions. This app supports that.
First, the config/api.php
Then, you need to configure the middleware. Instead of using api.v:1
, you now have to specify the name: api.v:3,desktop
.
Then the rest works as explained before.
Testing
Run the tests with:
Credits
- Adel KEDJOUR
- Juan Pablo Barreto
- All Contributors
License
The MIT License (MIT). Please see License File for more information.