Download the PHP package zozlak/rest without Composer
On this page you can find all versions of the php package zozlak/rest. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package rest
Short Description REST controller and endpoint classes
License MIT
Homepage https://github.com/zozlak/rest
Informations about the package rest
REST
Set of PHP classes for REST APIs creation:
zozlak\rest\HttpController
provides routing to the right endpoint class and handles errorszozlak\rest\HttpEndpoint
provides a base class for endpoints implementation
Features
- Simple and explicit endpoints implementation.
- Builtin support for HTTP basic auth.
- Convenient support for bigger then allowed PHP memory JSON output.
- Automatic handling of OPTION method.
Installation
The easiest way to install is by using composer:
-
prepare a
composer.json
file: - obtain the Composer
- run
php composer.phar install
The library should be now installed in thevendor
directory. - Include Composer's autoloader in your code by adding the
require_once 'vendor/autoload.php';
line at the beginning of your code.
Sample usage
Lets assume you want to create a RESTfull API providing following endpoints:
http://yourDomain/person
a collection supporting GET and POST methodshttp://yourDomain/person/{id}
resources supporting GET, PUT and DELETE methodshttp://yourDomain/project
a collection supporting GET and POST methodshttp://yourDomain/project/{id}
resources supporting GET, PUT and DELETE methods
HTTP server configuration
At first you will probably want to configure your HTTP server to redirect requests coming to your API to one PHP file.
Sample set of rules for Apache (to put into the .htaccess
file or into a <VirtualHost>
configuration directive) would be:
Endpoint classes
zozlak\rest\HttpEndpoint
is a base class for implementing REST API endpoints.
It provides a default implementation (emit HTTP 501 method not implemented
error code) for all HTTP methods for both resource and collection endpoints (e.g. both http://yourDomain/person
and http://yourDomain/person/{id}
). For resource endpoint method names simply follow HTTP method names (get()
, put()
, etc.) and for the collection endpoint they are suffixed with a collection
(getCollection()
, postCollection()
, etc.).
The dfault implementation of the OPTION
method checks which methods are implemented in your class and emits the Allow
header value accordingly (and the 404 Not Found
if none implemented).
You should derive your class from the zozlak\rest\HttpEndpoint
one and override methods with useful implementations.
A class name should follow the name of the last API endpoint segment (skipping {id}
segments) converted to CameCase, e.g.:
http://yourDomain/person
,http://yourDomain/person/{id}
orhttp://yourDomain/pErSoN/{id}
will be handled by thePerson
class.http://yourDomain/person/{id}/order
orhttp://yourDomain/person/{id}/order/{id}
will be handled by theOrder
class.
This means we must create two classes: Person
and Project
.
Lets assume you will put their code into the src
directory and follow the PSR-4 naming rules (meaning file name follows class name).
src\Person.php
src\Project.php
Just copy-paste-adapt the src\Person.php
.
index.php
Test your API
Try to access your API endpoints, e.g. with curl:
curl -i -X GET http://yourDomain/person
- 200 OK "you executed a GET action on a person collection"curl -i -X GET http://yourDomain/person/5
- 200 OK "you executed a GET action on a person resource with id 5"curl -i -X PUT http://yourDomain/person/5
- 200 OK "you executed a PUT action on a person resource with id 5"curl -i -X PUT http://yourDomain/person
- 501 Not implemented
Advanced topics
Authorization
To check credentials provided by the user use getAuthUser()
and getAuthPswd()
in your endpoint's class code.
If the credentials are wrong simply throw the zozlak\rest\UnauthorizedException
exception.
Example:
Changing HTTP status code and headers
Use the HeadersFormatter
object passed to all methods to alter response HTTP status and headers.
Lets assume you want to return for the POST method run on a collection with a 201 Created
response code and a Location
header pointing to the newly created item:
There is also a shorthand for redirects:
Returning non-JSON data
You shouldn't directly return data with echo
, etc. because it can break some library features (precisely after printing more then PHP output buffer bytes the PHP will automatically issue HTTP 200 status code with default set of HTTP headers and all the settings made to the HeadersFormatter
will be discarded).
There are two helper methods of the DataFormatter
object passed all methods helping you to deal with non-JSON data:
raw($data, $contentType, $filename = null)
- returns an arbitrary string with a givenContent-Type
header. If the$filename
parameter is provided, a corresponsingContent-Disposition: attachment; filename="$filename"
header is emitted.file($path, $contentType = null, $filename = null)
- returns a given file. By default theContent-Type
header value is guessed with themime_content_type()
PHP function but you can override it by passing thecontentType
parameter value. If the$filename
parameter is provided, a corresponsingContent-Disposition: attachment; filename="$filename"
header is emitted.
To alter HTTP status code and/or add other headers, use the setStatus()
and setHeader()
methods on the HeadersFormatter
object.
Example: