Download the PHP package coreproc/api-builder without Composer
On this page you can find all versions of the php package coreproc/api-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download coreproc/api-builder
More information about coreproc/api-builder
Files in coreproc/api-builder
Package api-builder
Short Description API builder for Laravel
License MIT
Homepage https://github.com/coreproc/api-builder
Informations about the package api-builder
API Builder for Laravel
API builder for Laravel aims to provide a quick and standard way of creating APIs. When you create an API for your resource using this package, you will get the standard CRUD endpoints and is flexible enough to adapt to the needs of your business logic.
Installation
You can install the package via composer:
Usage
To start using API builder, simply create a controller, have it extend the ApiBuilderController
class, then define the
necessary information regarding the resource. Here is what a basic controller should look like:
This controller assumes that you have a Post
model and a PostTransformer
transformer. If you don't know about
transformers, you can find more information here.
Now, to make the above controller accessible, all we need to do is define the route in routes/api.php
.
Once defined in your routes, you will now get the following endpoints:
Querying / Filtering
One of the main points of this package is to have a querying/filtering feature capable enough so that API consumers can have complete control over what they are looking for.
Inspired by GraphQL, the same way of adding operands to the parameters have been applied.
For example, if you want to query for all the Posts from a particular user ID, you would do this:
But if you wanted to query for all Posts where the user ID is greater than 5 (for example), you would do it like this:
The same would be applied for all the parameters defined in your $allowedParams
variable of your controller. You are
required to define which parameters are allowed to be passed through the query if you want to use this feature.
Here is another example. If you would like to get Posts where the title contains the word "test", here is how you would do it:
Here is the complete list of operands that you can use:
Now if you wanted to modify / apply a filter when indexing the resource by default, you can do so by overriding the
indexQuery()
method in your controller:
By doing the above, indexing the resource will only yeild results that have the user ID of the currently logged in user. You can do additional logic here as well.
Dates
Querying / Filtering of dates are also allowed and operands can be used as well. However, you will have to define the fields which are dates:
Once that is defined, you can pass any value that Carbon::parse()
can parse. For example:
Sorting
Sorting is also possible when going through the index of your resource. Here is an example:
This will sort all Posts by their published date in ascending order. To change the direction of the order, pass the
desc
value along like so:
Please note that this package will be reserving the sort
keyword for its sorting feature. (Roadmap: make this
configurable)
Null values
To pass null values, this package reserves the null
value to the query values. Everything that is passed with the
null
string will be converted into a NULL value in the backend. Here is an example on how to use it:
Limiting results
You can also limit your results by passing the limit
parameter:
Pagination
By default the index of the resource returns a paginated result. The default number of results per page is 15. (Roadmap: make this configurable)
To increase the number of results per page, you can pass the per_page
parameter:
Pages can be navigated by defining the page number:
Creating a resource
// TODO
Creation Rules
// TODO
Viewing a resource
// TODO
Updating a resource
// TODO
Update rules
// TODO
Deleting a resource
// TODO
Authorization
// TODO
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
About CoreProc
CoreProc is a software development company that provides software development services to startups, digital/ad agencies, and enterprises.
Learn more about us on our website.
Credits
- Chris Bautista
- All Contributors
License
The MIT License (MIT). Please see License File for more information.