Download the PHP package juampi92/api-resources without Composer
On this page you can find all versions of the php package juampi92/api-resources. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download juampi92/api-resources
More information about juampi92/api-resources
Files in juampi92/api-resources
Package api-resources
Short Description Manage your resources maintaining API versioning
License MIT
Homepage https://github.com/juampi92/api-resources
Informations about the package api-resources
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.
The idea behing this
A while back I faced this API versioning problem, so I wrote this medium post with my solution and this package reflects this.
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 versionate 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.
If you wanna use a ResourceCollection, you might wanna rewrite the collects()
method.
This way, the ResourceCollection will always have the correct class.
resolveClassname
will try to use the current version of the class, but if it's not possible, will use the latest.
Nested resources
To take advantage of the fallback functionality, it's recomended 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 diferent 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.
API Route
Sometimes you must return a route url on the api response. If you wanna keep the api version (which is always the current version), api-resources has the solution for you.
With this we have api.v1.auth.login
and api.v2.auth.login
when creating a new version.
Now just do api_route('api.auth.login')
, and it will output /api/v1/auth/login
or /api/v2/auth/login
accordingly.
How it works
It's grabbing the config api.resources
and doing a strtolower, so if you have 'resources' => 'App'
, will transform app.auth.login
into app.v1.auth.login
.
If you need to customize it, add a new config entry in config/api.php
like this:
If works with multiple APIs as explained before.
Testing
Run the tests with:
Credits
- Juan Pablo Barreto
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of api-resources with dependencies
illuminate/support Version ^7.0|^8.0|^9.0|^10.0
illuminate/http Version ^7.0|^8.0|^9.0|^10.0