Skip to content

ruru-ink/laravel-api-controller

 
 

Repository files navigation

Laravel Api Controller

For Laravel 5 Build Status Coverage Status Packagist Packagist Packagist Github Issues

Basic CRUD API Methods that can be extended for your models by default has a list, show, update, add and delete endpoint to interact with your model.

Installation

Install via composer

composer require phpsa/laravel-api-controller

Publish Configuration File (optional - if you need to change any of the default configurations)

php artisan vendor:publish --provider="Phpsa\LaravelApiController\ServiceProvider" --tag="config"

Usage

Generate a new Api Controller, Repository and Route via php artisan make:api {ModelName}

This will create a Api/ModelNameControlelr for you and you will have the basic routes in place as follows:

  • GET api/v1/{model_name} - list all/paged/filtered (index)
  • GET api/v1/{model_name}/$id - Show a specified id (show)
  • POST api/v1/{model_name} - Insert a new record (store)
  • PUT api/v1/{model_name}/$id - Update an existing record (update)
  • DELETE api/v1/{model_name}/$id - Delete an existing record (destroy)

You can override the methods by simply putting in your own methods to override - method names in braces above

Filtering

For the get command you can filter by using the following url patterns

Seperator Description Example Result
= Equals ?filter[field]=hello select ... where field = 'hello'
!= Not Equals ?filter[field!]=hello select ... where field != 'hello'
<> Not Equals (alt) ?filter[field<>]=hello select ... where field != 'hello'
> Greater Than ?filter[field>]=5 select ... where field > 5
>= Greater Or Equal to ?filter[field=>]=5 select ... where field >= 5
< Less Than ?filter[field<]=5 select ... where field <> 5
<= Less Or Equal to ?filter[field=<]=5 select ... where field <= 5
~ Contains (LIKE with wildcard on both sides) ?filter[field~]=hello select ... where field like '%hello%'
^ Starts with (LIKE with wildcard on end) ?filter[field^]=hello select ... where field like 'hello%'
$ Ends with (LIKE with wildcard on start) ?filter[field$]=hello select ... where field like 'hello%'
!~ Not Contains (LIKE with wildcard on both sides) ?filter[field!~]=hello select ... where field not like '%hello%'
!^ Not Starts with (LIKE with wildcard on end) ?filter[field!^]=hello select ... where field not like 'hello%'
!$ Not Ends with (LIKE with wildcard on start) ?filter[field!$]=hello select ... where field not like 'hello%'

Fields, Relationships, Sorting & Pagination

Fields

By default all fields are returned, you can limit that to specific fields in the following ways:

  • Api Controller parameter $defaultFields default as protected $defaultFields = ['*']; - switch to include an array of fields
  • fields param in url querystring: ie fields=id,name,age = will only return those, this will also override the above.

Relationships

  • Using the relationships defined in your models, you can pass a comma delimited list eg with=join1,join2 which will return those joins (one or many)

Sorting

  • Sorts can be passed as comma list aswell, ie sort=age asc or sort=age asc,name desc,eyes - generates sql of sort age asc and sort age asc, name desc, eyes asc respectively
  • Default sort can also be added on the controller using by overrideing the protected $defaultSort = null; parameter

Pagination

  • pagination can be enabled/disbled on the controller by overriding the protected $defaultLimit = 25; on the controller
  • pagination can also be passed via the url using limit=xx&page=y
  • pagination can also be limited to a max per page by overriding the protected $maximumLimit = false; parameter

Validation

  • When Posting a new record, validation can be done by adding a rulesForCreate method to your controller returning an array eg
[
    'email' => 'required|email',
    'games' => 'required|numeric',
]

see https://laravel.com/docs/5.8/validation#conditionally-adding-rules

  • for updating a record, add a method rulesForUpdate per above.

Defaults

The following parameters are set in the Base Api controller and can be overwritten by your Controller on a case by case basis:

  • protected $resourceKeySingular = 'data'; Resource key for an item.
  • protected $resourceKeyPlural = 'data'; Resource key for a collection.
  • protected $defaultFields = ['*']; Default Fields to respond with
  • protected $defaultSort = null; Set the default sorting for queries.
  • protected $defaultLimit = 25; Number of items displayed at once if not specified. (0 = maximumLimit)
  • protected $maximumLimit = 0; Maximum limit that can be set via $_GET['limit']. - this ties in with the defaultLimit aswell, and if wanting to disable pagination , both should be 0. ) will allow all records to be returned in a single call.
  • protected $unguard = false; Do we need to unguard the model before create/update?

Security

If you discover any security related issues, please email instead of using the issue tracker.

Credits

About

API Boilerplate for laravel-5-boilerplate

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%