Download the PHP package tagged/rest without Composer

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

Restful routing/controllers

Tagged/Rest is a small framework for creating restful api's in object oriented controllers rather than callbacks. It comes with powerful validation from the json schema project, and api methods both respond to http requests and can be called internally as normal object methods. There's also a mechanism to dynamically generate documentation for all api endpoints for free, just hit the url with an OPTIONS request.

The framework is focused only on handling http requests, leaving the rest of the project for other frameworks or existing code.

Really, this is just a wrapper around Klein that allows for easy construction of restful urls and controllers.

A url maps to a controller based on a routes mapping, and the controller handles the http request/response.

Routes

A route looks something like /owners/[a:name]/dogs.

The value in square brackets is matched and passed to the controller as query parameters.

A request to /owners/george/dogs?color=brown to the route above becomes

A route with a parameter as the last part of the url path is considered a resource route, and a route with a static name as the last part is a collection. Above, the dogs route would be a collection and photos a resource.

Routes support many features such as optional values and validation. See [https://github.com/chriso/klein.php#routing]() for more about route syntax.


The router takes a parameter that is an array of routes mapped to a controller, like

where the key is the route path and the value a namespace. If instead of a string, the value is an array, then the routes in that array are nested under the parent path. The router also takes a parameter defining a route namespace and another one defining the controller namespace.

A special syntax allows for mapping to a specific method for that route that's not one of the defaults. If the right hand side looks like "controllerClass::methodName", then a POST to the route on the left side will be handled by that method. In the example above, a post to /health/echo will be handled by the method 'showParams()' in the health controller.

Here's an example using the routing array above. If the route namespace /api/v1, the controller namespace /app/controllers, then routes generated by the config above are:

The http methods supported by each route are determined by looking at the controller. Routes are either handled as a collection or a resource. A resource acts on a single record (database row), and a collection acts on many records (database table). In the example above, requests to /api/v1/users/[i:userid]/photos would be handled by the controller as a collection, and /api/v1/users/[i:userid]/photos/[i:photoid] as a resource. This is determined by the convention of resource routes ending in an id/paramter (/[i:photoid]) and collections ending in the name of the collection (/photos).

Validation

Validation is done using a json schema validator. The query/form parameters are treated as a nested json structure. This structure comes from php's built in query deserialization. Query params like '?item[key]=value' are serialized as an array like .

This is fed into a validator that ensures the query parameters are structured how the API expects. For more info on json schema syntax and options, check http://json-schema.org/.

The schemas for each controller function are registered in the constructor.

Controllers

Controllers handle the http request. A handler method expects to be passed a stdClass object. The value returned by the method becomes the body of the http request.

The default mapping is below, but can be overridden. Methods not implemented become HTTP 405 errors when requested.

Every controller method is passed a response and request object. Check https://github.com/chriso/klein.php/wiki/Api for documentation.

HTTP method Controller method Expected action
GET (resource) fetch() get the requested item (no side effects)
PUT (resource) update() update the requested item (idempotent)
DELETE (resource) delete() delete the requested item (idempotent)
GET (collection) index() used as an index function for simple urls
GET (collection) find() list all items matching the query (no side effects)
POST (collection) create() create a new item
PUT (collection) bulkUpdate() update a set of items (idempotent)
DELETE (collection) deleteAll() delete all items (idempotent)

Note that there are two handlers for GET requests on a collection. This is just so that controller methods can have better names. Use find when the method is going to be returning a collection of data. Index is better used for responding to basic url's, like a health check or html page.

A controller may register as many methods as it likes to respond to the same http action, which is usesful for custom actions. Custom methods can be registered in the constructor. Also, multiple controllers may respond to the same route.

Controller example:

An example url for this controller is domain.com/api/v1/users/5/photos?pagination[page]=2&pagination[size]=25

Note the form params, this is the structure that the validator will validate against. See validation.

Controllers can add a post-response method to format the output. A controller implementing _formatOutput will be able to serve different format output. The format is taken from the extension of the url. A url like www.mysite.com/page.html would add the query parameter , and www.mysite.com/page.html would add the query parameter . If no format is given then the value is null.

Usage

Put this in an index.php file:

It's probably better to put all that in a config file and load them in the index file.

Calling api methods internally

All the api methods can be called without making an http request. Two methods to do this are directly and wrapped. When wrapped the filters and schema validation are applied, and methods can be called with arrays instead of stdClass objects.

Wrapped, this will go through the same filtering as a web request.

Of course, the other method is to instantiate the object and call the methods as a normal object. Calling a method directly kinda sucks since all the controllers expect stdClass objects.

Auto API Documentaion

Documentation is important for an API, and this framework provides a mechanism to automatically produce documentation for free.

Hitting any api with an OPTIONS request will return an html page with the documentation for that api. The documentation comes from both doc comments on the class and from the schema specified for the api methods.

There are plans to add a single endpoint that acts as a collection of this documentation, so that all apis are easily discoverable.

Because the documentation is generated dynamically it will always match the code, rather than requiring a job to be run to keep the documentation and api in sync.

Notes

This project has not been performance tuned, but should run similar to Klein. Most of the big work is done mapping the routes to controller callbacks and building the router, after that it's basically a Klein project.

Currently, for some production APIs, we're just building the routes on every request for about 60ms overhead. For a high scale service, that's something you'd want to cache. Caching is not built in, but the router is built to be easily cached if need be.

Dependencies

Klein (for routing) https://github.com/chriso/klein.php geraintluff's coercive json validator https://github.com/geraintluff/jsv4-php

For testing requires Phockito and php-unit.


All versions of rest with dependencies

PHP Build Version
Package Version
Requires php Version >=5.3.0
klein/klein Version dev-master
geraintluff/jsv4 Version dev-master
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 tagged/rest contains the following files

Loading the files please wait ....