PHPackages                             zozlak/rest - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. zozlak/rest

AbandonedArchivedLibrary[HTTP &amp; Networking](/categories/http)

zozlak/rest
===========

REST controller and endpoint classes

6.0.1(2y ago)0210MITPHPPHP &gt;=8CI failing

Since Jun 8Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/zozlak/rest)[ Packagist](https://packagist.org/packages/zozlak/rest)[ Docs](https://github.com/zozlak/rest)[ RSS](/packages/zozlak-rest/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (10)Dependencies (3)Versions (31)Used By (0)

REST
====

[](#rest)

Set of PHP classes for REST APIs creation:

- `zozlak\rest\HttpController` provides routing to the right endpoint class and handles errors
- `zozlak\rest\HttpEndpoint` provides a base class for endpoints implementation

Features
--------

[](#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
------------

[](#installation)

The easiest way to install is by using composer:

- prepare a `composer.json` file:

```
{
    "require": {
        "zozlak/rest": "*"
    }
}

```

- obtain [the Composer](https://getcomposer.org/download/)
- run `php composer.phar install`
    The library should be now installed in the `vendor` 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
------------

[](#sample-usage)

Lets assume you want to create a RESTfull API providing following endpoints:

- `http://yourDomain/person` a collection supporting GET and POST methods
- `http://yourDomain/person/{id}` resources supporting GET, PUT and DELETE methods
- `http://yourDomain/project` a collection supporting GET and POST methods
- `http://yourDomain/project/{id}` resources supporting GET, PUT and DELETE methods

### HTTP server configuration

[](#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 `` configuration directive) would be:

```
RewriteEngine on

# handle requests for existing files and directories as usual
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# redirect everyting else to index.php
RewriteRule ^(.*)$ index.php [QSA]

```

### Endpoint classes

[](#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}` or `http://yourDomain/pErSoN/{id}` will be handled by the `Person` class.
- `http://yourDomain/person/{id}/order` or `http://yourDomain/person/{id}/order/{id}` will be handled by the `Order` 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

[](#srcpersonphp)

```
