Download the PHP package fadugyamfi/laravel-api-base without Composer
On this page you can find all versions of the php package fadugyamfi/laravel-api-base. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download fadugyamfi/laravel-api-base
More information about fadugyamfi/laravel-api-base
Files in fadugyamfi/laravel-api-base
Package laravel-api-base
Short Description Laravel Package for easy RESTful API development
License MIT
Informations about the package laravel-api-base
laravel-api-base
Laravel Package For Easy RESTful API Development
About
This library enables you to significantly speed up the development of your RESTful API with Laravel by providing you the base Controller, Request, Resource and Model with all the CRUD functionality you will need as well as data search and count endpoints.
Installation
The preferred method of installation is via [Packagist][] and [Composer][]. Run the following command to install the package and add it as a requirement to your project's composer.json
:
Latest Laravel versions have auto dicovery and automatically add service providers, however if you're using Laravel 5.4.x and below, remember to add it to providers
array at /app/config/app.php
:
Lastly, and most importantly, this package provides an updated Router
, i.e. LaravelApiBase\Services\ApiRouter
that you must configure in your app/bootstrap/app.php
file to get the full benefits of the api. You need to add the following code in the file.
Using the Library
These examples assume you already have your database setup and just need to wire up your Models, Controllers and Routes.
Making Requests to the API
Once your API endpoints are configured you can make the following requests easily
Standard RESTful API Requests
Querying and Paginating Results
This is where the package shines and enables you to flexible work with your API Endpoints
Searching/Filtering Data
You can search and filter data using any field that is included in the model $fillables
and apply operators to them to enhance the queries
Available Search Operators
_eq
- Equal Operator.DEFAULT
_like
- Use aLIKE {term}%
operation_has
- Use aLIKE %{term}%
operation_sw
- Starts With. Use aLIKE {term}%
operation_ew
- Ends With. Use aLIKE %{term}
operation_gt
- Greater Than Operator_gte
- Greater Than or Equal To Operator_lt
- Less Than Operator_lte
- Less Than or Equal To Operatot_in
- IN Operator_notIn
- NOT IN Operator_not
- NOT Operator_isNull
- IS NULL Operator_isNotNull
- IS NOT NULL Operator
Working With Associated Models
You can return any kind of model association that the with()
operation supports in Laravel Eloquent Models
Sorting Results
You can also sort results by any field when querying
Configuring Your Application
Now you know what is possible, let's show you how to set everything up so you can try this out. This section assumes you have a database table called todos
. We'll set up the Model, Controller, FormRequest and JSONResource for that specific endpoint
Setting up your Model
To access the data you will need a model.
You can also implement the ApiModelInterface
and use the ApiModelBehavior
Trait and for existing Models that cannot extend the
the ApiModel
class directly for other reasons, e.g. the default User
model
Observables Are Encouraged
When working with your API Models, we encourage you to use Observers to listen and react to events in your model for the cleanest code possible. Example:
Then in your app\Providers\EventServiceProvider.php
file, you can connect your Observer to your Model
Setting up Requests for Input Validations
When creating or updating a record, we often need to validate the inputs. Creating a Request object will enable this to happen behind the scenes.
Setting up a JSONResource to format your responses
You can customize the response that comes back to your users by creating a subclass of LaravelApiBase\Http\Resources\APIResource
Setting up your Controller
By simply specifying the model to use, the controller will infer any resources or request objects needed and use them for all your restful endpoints
If your Request and Resource classes do not live in the default directories, i.e. App\Http\Requests
and App\Http\Resources
, you can override the automatic path resolution by in your Controller __construct()
function
Configuring the Routes for your Todo endpoint
Once you have everything setup, you can now add a route that will make your resource available. You should potentially add these to
the routes/api.php
file.
Generating API Documentation
This library is designed to work with the Scribe Documentation Generator. Install the library and run the following command to generate your documentation.
Submitting bugs and feature requests
Bugs and feature request are tracked on GitHub
Author
Francis Adu-Gyamfi - https://www.linkedin.com/in/francis-adu-gyamfi-3b782716/
See also the list of contributors which participated in this project.
License
Laravel API Base is licensed under the MIT License - see the LICENSE
file for details
Acknowledgements
This library was possible due to the team of developers at Matrix Designs who inspired its creation.
All versions of laravel-api-base with dependencies
illuminate/support Version ^5.2|^6|^7|^8|^9|^10|^11