Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/bitnbytesio/router

Fast and simple request router for PHP7
https://github.com/bitnbytesio/router

php7 php71 router routing

Last synced: 21 days ago
JSON representation

Fast and simple request router for PHP7

Awesome Lists containing this project

README

        

Router - Simple/Fast request router for PHP7
=======================================

Install
-------

To install with composer:

```sh
composer require artisangang/router
```

Requires PHP 7.

Usage
-----

```php
storage( $storage );

// lock routes in production mode for performance
$routeManager->lock();

if (isset($_ENV["development"])) {
$routeManager->unlock();
}

//product routes
$p = $routeManager->router();
$p->use('GET /product/', 'IndexController@productIndex');
$p->use('GET /product/:id', 'IndexController@productSingle');
$p->use('POST /product', 'IndexController.productCreate');
$p->use('PUT /product/:id', 'IndexController.productUpdate');
$p->use('PATCH /product/:id', 'IndexController.productPatch');
$p->use('DELETE /product/:id', 'IndexController.productDelete');

// category routes
$c = $routeManager->router();
$c->use('GET /category', 'IndexController.categoryIndex');
$c->use('POST /category', 'IndexController.categoryCreate');
$c->use('PUT /category/:id', 'IndexController.categoryUpdate');
$c->use('PATCH /category/:id', 'IndexController.categoryPatch');
$c->use('DELETE /category/:id', 'IndexController.categoryDelete');

// shop routes
$c->get('/shop', 'IndexController.shopIndex');
$c->post('/shop', 'IndexController.shopCreate');
$c->put('/shop/:id', 'IndexController.shopUpdate');
$c->patch('/shop/:id', 'IndexController.shopPatch');
$c->delete('/shop/:id', 'deleteShop');

// group routes in v1 prefix
$r = $routeManager->router('v1');
// add routes to group
$r->child($p, $c);

// create another group with prefic api
$a = $routeManager->router('api');
// add route to this gorup
$a->child($r);

// find route match based on current request uri, this will return Router\Commands\HttpCommand instance if match found
$request = $routeManager->match($a);

if (!$request) {
die('404,Page not found!');
}

// extract params from uri
// for example with uri /product/1 matched with pattern /product/:id will return ['id' => 1]
$params = $request->params($routeManager->requestUri);

```