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

On this page you can find all versions of the php package camilord/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

Fork from phpsa/laravel-api-controller

This repository is required from one of my work projects I maintained and unfortunately the maintainer remove the hasRepository and BaseRepository after version 2.0.0.

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

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

Usage

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

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

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: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

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

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.

Fields, Relationships, Sorting & Pagination

Fields

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

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 php Version ^8.0
laravel/framework Version ^6.0|^7.0|^8.0|^9.0|^10.0
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 camilord/laravel-api-controller contains the following files

Loading the files please wait ....