Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/morphable/simplerouting

Simple routing component
https://github.com/morphable/simplerouting

component php routing simple

Last synced: 7 days ago
JSON representation

Simple routing component

Awesome Lists containing this project

README

        

# SimpleRouting

A simple component that takes care of routing.

## Installing

```terminal
$ composer require morphable/simple-routing
```

## Usage

```php
sendResponse('UserId = ' . $req->getParam('user_id'), 200);
});

// A POST route with middleware
$route2 = new Route('POST', '/user/:user_id/update', function ($req, $res) {
return $res->sendResponse('Welcome user 2!');
}, [
// middlewares
function ($req, $res) {
if ($req->getParam('user_id') != 2) {
return $res->sendResponse('Forbidden', 403);
}
}
]);

SimpleRouting::add('user_detail', $route);
SimpleRouting::add('user_update', $route2);

try {
SimpleRouting::execute();
} catch (\Morphable\SimpleRouting\Exception\RouteNotFound $e) {
// catch 404
die('404');
}

```

## Builders and callbacks
Better callbacks and builders!

Callback can now be anything that works with ```call_user_func()```

```php

use \Morphable\SimpleRouting\Builder;

$route = (new Builder())
->setMethod('GET')
->setRoute('/user/:userId')
->setCallback($callback)
->setMiddleware([$callback])
->build();

$route = Builder::fromArray([
'method' => 'GET',
'route' => '/user/:userId',
'callback' => $callback,
'middleware' => $callback
]);

```

## Contributing
- Follow PSR-2 and the .editorconfig
- Start namespaces with \Morphable\SimpleRouting
- Make tests