Download the PHP package mleczek/laravel-rest without Composer

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

Laravel REST Package

Latest Stable Version Build Status License

Laravel package with a set of tools helpful for building REST API.

Installation

To install this package you will need:

Require this package with composer:

In config/app.php add the RestServiceProvider:

Configuration

Publish the package configuration and ContextServiceProvider:

This command will create 2 files for you:

Register new local copy of the ContextServiceProvider in the config/app.php file:

Usage

Query params

Supplied by the client. They control the format of the response most often by narrowing result.

With

Include related data in response:

In the background the library will attach related models using Eloquent defined relations, which is quite similar to calling $quer->with(['messages', 'permissions']).

By default all relations are disabled, which means that you have to explicitly define which relations can be used in API call. You can set up this in the previously published ContextServiceProvider:

If you'd like to do some policy checks then you can define context class. In this class you can create methods which name is equal to the relation name. Whenever you try to access this relation the new instance of this class will be created and the result of the method will determine if the relation can be used or not.

Of course you have to register that context in the ContextServiceProvider:

Now if someone without root access call the with=messages then nothing will happen. If you'd like you can throw 401 or 403 response code from the context class.

In UserWithContext class and other context classes you can inject your dependencies in the constructor, because class is resolved using service container.

Offset

Skip n first items:

You can use this as well for the related data:

Limit

Limit results to n items:

You can use this as well for the related data:

Fields

Get only specified fields:

You can use this as well for the related data:

If you specify fields only for the primary model then all fields will be retrieved for the related one:

Above example will return first_name, last_name and all columns for the messages model (eq. id, author_id, recipient_id, content). This helps in finding a sub-optimal query - if you don't want any field from the related model then just simply remove redundant values from the with query param.

Sort

Return results in specified order:

You can use this as well for the related data:

By default no sort methods are available, which means that you have to explicitly define sort that can be used for specific model. You can set up this in the previously published ContextServiceProvider:

Then you have to create context class. Sort name will be converted to method name using camelCase style (eq. last_name_desc will call lastNameDesc method). As a first argument you will receive the Illuminate\Database\Query\Builder.

Now you can sort messages using latest method in any context:

In MessageSortContext class and other context classes you can inject your dependencies in the constructor, because class is resolved using service container.

Filter

Put constraint on request:

Unlike sort param the filter query param can also accept arguments:

You can use this as well for the related data:

By default no filter methods are available, which means that you have to explicitly define filters that can be used for specific model. You can set up this in the previously published ContextServiceProvider:

Then you have to create context class. Filter name will be converted to method name using camelCase style (eq. last_name_in will call lastNameIn method). As a first argument you will receive the Illuminate\Database\Query\Builder.

Now you can filter users using scoreAbove method in any context:

In UserFilterContext class and other context classes you can inject your dependencies in the constructor, because class is resolved using service container.

Responses

Library extends the Response class using some helpful macros.

Item

Response single model item with response code 200 OK:

This macro will use the fields and with query param.

This is not recommended to use with, select and addSelect on the $query parameter passed to the method. After all if you would like to do this the behavior it is as follows:

Often you will need to do some operations using retrieved model, in this case use rest() helper funtion:

The above example has one major defect, the second argument passed to the authorize can contain only fields specified in the fields query param. The better solution is to retrieve the whole model, call authorize method and then apply the query param transformations:

Summarizing, the response()->item() macro can accept the Illuminate\Database\Eloquent\Model or Illuminate\Database\Eloquent\Builder object.

Collection

Response collection of models with response code 206 Partial Content:

This macro will use the fields, sort, filter, offset, limit and with query param. Again, using orderBy, select, addSelect, limit/take, offset/skip methods on the $query argument is not recommended, but fell free to add some constraints using where method.

If you will need to make some operations before returning response you can use rest() helper function:

Accepted

Empty response with status code 202 Accepted.

No Content

Empty response with status code 204 No Content.

Created

Response created model with status code 201 Created. If $location is specified then appropriate Location header will be added to the response.

Updated

Response updated model (if provided) with status code 200 OK.

Patched

Response part of updated model (if provided) with status code 200 OK.

Deleted

Empty response with status code 204 No Content.

Tips and tricks

Default Context

By default library implements 2 context classes:

These context can be used with any class and allow sorting and filtering using fillable attributes. Example usage for the default User model:

It's recommended to use only for the dev purposes. In future releases implementation will change in order to prevent accidental security vulnerabilities (like the above one with password column). Any ideas are welcome.

Contributing

Thank you for considering contributing! If you would like to fix a bug or propose a new feature, you can submit a Pull Request.

License

The library is licensed under the MIT license.


All versions of laravel-rest with dependencies

PHP Build Version
Package Version
Requires illuminate/support Version ^5.3
illuminate/http Version ^5.3
illuminate/database Version ^5.3
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 mleczek/laravel-rest contains the following files

Loading the files please wait ....