Download the PHP package sehrgut/laravel5-api without Composer

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

laravel5-api

Gitter Chat Travis Build Status StyleCI Status Code Climate

A modular controller for exposing your Laravel 5 Eloquent models as a REST API. All you need to do is create one subclass of the controller per model and set up the routes.

Disclaimer: This is an early release! Do not use in production without extensive testing! The API is subject to change!

Please use Github Issues for bug reports and feature requests.

Table of Contents

Documentation

API Reference v0.7.8 (v0.6.5, v0.5.3, v0.4.2, v0.3.0)

Getting Started

Install Package

Create an Endpoint

Controller

Subclass SehrGut\Laravel5_Api\Controller and set the eloquent model your controller should expose. Example:

Routes

You now have a controller with the same handlers as a Laravel Resource Controller. Those methods can now be used to handle the following routes:

Mapping route parameters to model attributes

By default, it is assumed that there is an {id} parameter in all urls pointing to a single resource (show, update, destroy). This parameter is then used to find the corresponding model by its id attribute.

If your model's primary key or your route parameter have a different name than id, you need to manually map these in your Controller's $key_mapping. Example:

In the same manner, additional url parameters can be mapped to model attributes. This is especially useful when creating endpoints for nested resources like /api/v1/posts/{post_id}/comments/{comment_id}.

Validators & Transformers

You might want to create a Validator and a Transformer and assign them to your Model in your ModelMapping. More on how this works under Components.

Error Responses

In order to have Exceptions displayed correctly, make sure to handle SehrGut\Laravel5_Api\Exceptions\Exception in your app/Exceptions/Handler.php:

Structure

In a larger Project with several endpoints, it is reasonable to have a common BaseController where the ModelMapping is defined for all endpoints.

Example Directory Structure:

Components

The logic is divided up into smaller components, each with their own responsibility:

Controller

Available Handlers

Side-loading and counting relations

Side-loads and relationship counts can be added to the response by enumerating the names of the relation in the $relations or $counts properties on the controller. Both work recursively, allowing to side-load/count nested relations using dot-syntax.

Example:

Validator

In order to create a custom Validator for a model, you can subclass the Validator class and set the $rules array. After that, the Validator needs to be registered in the ModelMapping which is assigned to your Controller. Please refer to the ModelMapping section on how to do this. A Validator could look like this:

Transformer

To shape how your models are represented at the api, you can do some transformatios on the model while generating the response. This works the same way as with Validators. Just subclass Transformer and assign them to your models via the ModelMapping.

In your Transformer subclass, you can define the following attributes to customize the output:

Further, you can change the values of individual attributes of your models by defining a formatAttribute method on the Transformer where Attribute is the camel-case name of the attribute you want to transform. The method should accept a single argument (the original value) and return the transformed attribute. Example:

ModelMapping

The Controller asks the ModelMapping which Validator and Transformer it should use for each Model and their respective relations. If no Transformer/Validator is assigned to a model, the respctive defaults are returned (No validation, no transformation).

In order to apply custom Transformers or Validators to your models, you have to create a custom model mapping and assign it to your Controllers (preferrably via a common BaseController).

RequestAdapter

TBD

Customization

Plugins

Plugins are a way of "hooking into" and manipulating the behaviour of controller actions. They replace the old "hooks", used in versions ≤0.3

Usage

Plugins can be registered inside the Controller by specifying them in the $plugins property. The order of execution will be the order in which they are listed here.

Example:

Declaring Hooks on the Controller

Instead of using Plugins, a controller can also implement any of the Hooks itself, in order to influence the request/response lifecycle. In this case, it behaves the same as a Plugin:

  1. Declare that the controller implements the appropriate Hook Interface
  2. Declare the method required by the Interface

If a controller subscribes to a Hook this way, the Hook will first be called on the controller, before it gets handed through the Plugins.

Configuration

Some plugins have configurable options, that can be set through the controller. This can be done from inside the afterConstruct() method like so:

Please refer to the source code or API reference of the individual plugin to see the available configuration options.

Available Plugins

Please use the source for definite answers. Still, here's a (probably outdated) list of plugins:

For help on using the individual plugins, check their respective source files or the API reference!

Writing Plugins

A plugin is just a class that extends SehrGut\Laravel5_Api\Plugins\Plugin. It can implement one or more Hooks in order to influence the controller's behaviour.

Plugin Configuration

The base Plugin class provides a protected $config attribute, to store configuation options, which are settable through the controller's configurePlugin($name, $options) method. Use this feature to expose parameters of your plugin to the user (the person writing the controller). Retrieve config options inside the plugin with $this->config['option_name']. Default values should be set via the protected $default_config property.

Example:
Plugin Hooks

The available hooks are listed in the "Hooks" directory.

In order to use a hook, you simply have to declare that your plugin class implements the corresponding interface and then actually implement the appropriate method of that interface. Take a look at source code of the existing plugins to see how this is done.

Each hook interface declares exactly one method. The name of this method is the same as the interface, just with a lowercase first letter. Example: The Hook Interface AdaptResourceQuery declares a method named adaptResourceQuery.

Available Hooks

AdaptCollectionQuery::adaptCollectionQuery() Customize the query for fetching a resource collection (index action).

AdaptResourceQuery::adaptResourceQuery() Customize the query for fetching a single resource (show, update and destroy actions).

AdaptRelations::adaptRelations(array $relations) This hook receives an array of relations to be side-loaded with the queried model. Return the adapted array.

BeginAction::beginAction() This is the first hook in any action, right after $context->action was set.

AuthorizeAction::authorizeAction() Hook in here to perform authorization on action level. This hook is only called on the index and store actions.

AuthorizeResource::authorizeResource() Hook in here to perform authorization on a single resource. This hook is called from the show, update and destroy handler right after the resource was fetched from DB and stored into $this->resource.

FormatCollection::formatCollection() This hook receives a Collection of resources before they are transformed.

FormatResource::formatResource() This hook receives a single resource before it is transformed.

ResponseHeaders::responseHeaders() Hook in here to manipulate the response headers.

BeforeSave::beforeSave() Is called on every create and update action after the model has been filled from $context->input right before the call to $context->resource->save().

AfterSave::afterSave() On every create and update action after the call to $context->resource->save().

BeforeCreate::beforeCreate() Hook just before beforeSave() but only in the store action.

BeforeUpdate::beforeUpdate() Hook just before beforeSave() but only in the update action.

Controller / Plugin Context

Each plugin has a reference to the controller's Context object stored in Plugin::$context. The Context contains all relevant pieces of data, that a Plugin might need to read or write:

Deprecated: Hook Methods

Warning: Hooks are deprecated in favour of Plugins (see above), so be aware when using them: The methods listed below will soon be removed from the controller and substituted with appropriate plugin hooks. "Hook" in the context of a "Plugin" refers to an interface, rather than a controller method like in the old sense.

There are serveral hooks in the Controller which help you customizing its behaviour. All you need to do is implement the desired method in your controller. For details on the hooks please browse the code and refer to the API Reference.

makeModelMapping()

Dynamically customize the ModelMapping, for example based on Auth/Roles

makeRequestAdapter(Request $request)

Dynamically customize the RequestAdapter.

adaptRules(Array $rules)

Adapt the validation rules after fetching them from the validator. Return the adapted rules.

afterConstruct()

Last call in the controller's __construct() method.

Changelog

Please refer to CHANGELOG.md.

Compatibility

Testing

Tests are based on phpunit and use an in-memory sqlite database. As the tests rely on the laravel framework, composer (dev-) dependencies need to be installed first:

License

This software is licensed under the MIT License. See LICENSE.txt for details.


All versions of laravel5-api with dependencies

PHP Build Version
Package Version
Requires php Version >=5.4
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 sehrgut/laravel5-api contains the following files

Loading the files please wait ....