Download the PHP package phpsa/laravel-api-controller without Composer

On this page you can find all versions of the php package phpsa/laravel-api-controller. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-api-controller

Laravel Api Controller

[[TOC]]

For Laravel 9 to 10 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

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

Usage

CLI Commands

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

If you specify --soft-deletes option on make:api:controller it will also create an additional restore controller endpoint & route:

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

Events

Policies

Policies: https://laravel.com/docs/6.x/authorization#generating-policies

Generate with php artisan make:policy PostPolicy --model=Post

Query/Data modifiers in policies for the api endpoints

Resources / Collections (Transforming)

Resources: https://laravel.com/docs/6.x/eloquent-resources

Generate with php artisan make:apiresource UserResource and php artisan make:api:resource UserCollection

Change the Resource to extend from:

use Phpsa\LaravelApiController\Http\Resources\ApiResource for your resource use Phpsa\LaravelApiController\Http\Resources\ApiCollection for your resource collection

in your controller override the following params:

Snake vs Camel

Filtering

stable option that will be removed once experimental stable

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%'

In / Not In

You can pass to the filters an array of values ie: filter[user_id]=1||2||||4||7 or filter[user_id!]=55||33

Null / Not Null (introduced 1.23.0)

If you need to filter on whether a field is null or not null, you can use the filter param as of version 1.23.0 EG: filter[age]=NULL or filter[age!]=NULL. Note that NULL must be uppercase.

Older versions Add a scope to your model: eg

Add to your allowedScopes and can then be called in url as ?ageNull=1 for where null and ?ageNull=0 for where age not null

Experimental v5 currently

Seperator Description Example Result
empty / = / is / equals Equals ?filters[field]=hello / ?filters[field][is]=hello select ... where field = 'hello'
!= / !is / !equals / not_equals Not Equals ?filter[field][!is]=hello select ... where field != 'hello'
> / greater_than Greater Than ?filter[field][greater_than]=5 select ... where field > 5
>= / greater_than_or_equal_to / greater_or_equal Greater Or Equal to ?filter[field][greater_or_equal]=5 select ... where field >= 5
< / less_than Less Than ?filter[field][<]=5 select ... where field <> 5
<= / less_than_or_equal_to / less_or_equal Less Or Equal to ?filter[field][less_or_equal]=5 select ... where field <= 5
~ / contains Contains (LIKE with wildcard on both sides) ?filter[field][contains]=hello select ... where field like '%hello%'
^ / starts_with Starts with (LIKE with wildcard on end) ?filter[field][starts_with]=hello select ... where field like 'hello%'
$ / ends_with Ends with (LIKE with wildcard on start) ?filter[field][ends_with]=hello select ... where field like 'hello%'
!~ / !contains / not_contains Not Contains (LIKE with wildcard on both sides) ?filter[field][!contains]=hello select ... where field not like '%hello%'
!^ / !starts_with / not_starts_with Not Starts with (LIKE with wildcard on end) ?filter[field][!^]=hello select ... where field not like 'hello%'
!$ / !ends_with / not_ends_with Not Ends with (LIKE with wildcard on start) ?filter[field][!$]=hello select ... where field not like 'hello%'
in in ?filter[field][in]=1,2,3 select ... where field in(1,2,3)
not_in / !in NOT in ?filter[field][in]=1,2,3 select ... where field not in(1,2,3)
has has ?filter[field][has] select ... where exists(field join)
not_has / !has NOT has ?filter[field][!has] select ... where not exists (field join)

Enforced scopes / query filters on a controller override the

method in your controller to include any additional queries / scopes etc.

Requests

We have added a request macro to enable you to set these on your request as needed:

eg:

In your controller, we generally use request->all() for the filling of models. Should you wish to use a more strict option, you can opt into using validated values only by calling $this->setOnlyValidated() in your controller which will then use the request->validated() to get the data (Note: this means it will not take any merged information from postValidation).

Scopes

In addition to filtering, you can use Laravel's Eloquent Query Scopes to do more complex searches or filters. Simply add an $allowedScopes to your ApiResource, and that scope will be exposed as a query parameter.

Assuming you have a scopeFullname defined on your Eloquent Model, you can expose this scope to your API as follows:

Given the above $allowedScopes array, your API consumers will now be able to request ?fullname=John. The query parameter value will be passed to your scope function in your Eloquent Model.

Filtering on related models

You can easily filter using any related model that is configured for include. Simply specify ?filter[model.field]=123 in your query string. The same filter options above apply to related fields.

Grouped Filtering Scopes

filter_by_relation_group[a][name]=weight&filter_by_relation_group[a][value][>]=900&filter_by_relation_group[b][name]=color&filter_by_relation_group[b][value]=color

Fields, Relationships, Sorting & Pagination

Fields

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

Gated Response Fields

Gates can be used to control access to fields and related resources, by defining $gatedFields:

Each specified gate will be used to determine whether that set of fields will be included.

Each gate will be passed the resource as well as the user, so it can test whether the user should be allowed to access that specific resource.

Example gate definition:

Relationships

Simply add a protected static $mapResources to your Resource to define which resources to assign your related data. E.e., for a one to many relationship, you should specify a collection, and a one-to-one relationship specify the related resource directly. This will allow the API to properly format the related record.

For BelongsToMany or MorphToMany relationships, you can choose the sync strategy. By default, this will take an additive strategy. That is to say, related records sent will be ADDED to any existing related records. On a request-by-request basis, you can opt for a sync strategy which will remove the pivot for any related records not listed in the request. Note the actual related record will not be removed, just the pivot entry.

To opt for the sync behavaiour, set ?sync[field]=true in your request.

Sorting

Pagination

Validation

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

Defaults

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

Scopes

SoftDeleted Records

add the Phpsa\LaravelApiController\Model\Scopes\WithSoftDeletes trait to your model, add to your resource file:

you can now append withTrashed=1 or onlyTrashed=1 to your query.

Responses

you can override responses for each point by overriding the following protected methods:

Security

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

Credits

Sponsors


All versions of laravel-api-controller with dependencies

PHP Build Version
Package Version
Requires laravel/framework Version ^9.0|^10.0|^11.0
php Version ^8.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package phpsa/laravel-api-controller contains the following files

Loading the files please wait ....