PHPackages                             sergiosgc/rest-router - 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. sergiosgc/rest-router

ActiveLibrary

sergiosgc/rest-router
=====================

Filesystem based request router for HTTP verb-aware applications (RESTful)

10PHP

Since Apr 11Pushed 3y ago2 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

rest-router
===========

[](#rest-router)

Filesystem based request router for HTTP verb-aware applications (RESTful)

Objective
---------

[](#objective)

The usual PHP request routing pattern is that the script filename is obtained from the request URI, possibly after appending `index.php`. This router follows the same logic, with a step up the RESTful path. It uses the request HTTP verb (`GET`, `POST`, `PUT`, ...) to find out the correct script in the filesystem path that will handle the request.

Let's assume an application stored under the document root `/srv/www/myapp` receiving three requests:

1. A GET to `/customer/`
2. A POST to `/customer/`
3. A PUT to `/customer/10`

Under typical PHP routing, the first two requests will be handled by `/srv/www/myapp/customer/index.php` and the third one would require request rewriting at the webserver layer

Using this router, the requests will be handled by:

1. `/srv/www/myapp/customer/get.php`
2. `/srv/www/myapp/customer/post.php`
3. `/srv/www/myapp/customer/put.php` (extraction of the customer id is possible and covered in this readme)

This eases development of RESTful apps, while leveraging the application directory structure to map out the URL space.

Installation
------------

[](#installation)

The easiest way to install and use is via Composer. On your composer.json:

```
# composer.json
{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/sergiosgc/composer-fs-rest-router.git"
        }
    ],
    "require": {
        "sergiosgc/rest-router": "dev-master"
    }
}

```

Then issue either `composer update` or `composer install`.

Now, on your document root, create a catch-all script named index.php:

```
