> composer require lucid/mux
<?php
use Lucid\Mux\Route;
use Lucid\Mux\Routes;
$routes = new Routes;
$routes->add('index', new Route('/', 'Acme\FrontController@getIndex'));
<?php
use Lucid\Mux\RouteCollectionBuilder as Builder;
$builder = new Builder;
// adds a GET route
$builder->get('/', 'Acme\FrontController@getIndex');
// adds a POST route
$builder->post('/user', 'Acme\UserController@createUser');
// adds a UPDATE route
$builder->update('/user/{id}', 'Acme\UserController@updateUser');
// adds a DELETE route
$builder->delete('/user/{id}', 'Acme\UserController@deleteUser');
The router component takes a request context object to dispatch the corresponding routing action.
<?php
use Lucid\Mux\Router;
use Lucid\Mux\Request\Context as RequestContext;
$router = new Router($builder->getCollection());
$request = new RequestContext(
current(explode('?', $_SERVER['REQUEST_URI'])),
$_SERVER['REQUEST_METHOD']
);
$response = $router->dispatch($request);
You can easily create a requestcontext from an existing psr7 compatible
server request by using the Context::fromPsrRequest()
method.
<?php
$request = new RequestContext::fromPsrRequest($psrRequest);
<?php
$options = [
'id' => 12
];
$response = $router->route('user.delete', $options);
The router mostly relies on two main components:
- a handler dispatcher, which is responsible for finding and executing the given action (defined on the route object)
- a response mapper, which is capable of mapping the responsens to a desired type
By default, the handler dispatcher/resolver will check if the given handler is callable. If the handler is a string containing an @ symbol, it is assumed that the left hand side represents a classname and the right hand site a method.
If the handler resolver (Lucid\Mux\Handler\Resolver
by default) is constructed
with an instance of Interop\Container\ContainerInterface
it will also check if
the left hand side is a service registered by the di container.
<?php
use Lucid\Mux\Handler\Resolver;
use Lucid\Mux\Handler\Dispatcher;
$resolver = new Resolver($container)
$dispatcher = new Dispatcher($resolver);
By default, the response mapper is a simple passthrough mapper. However it's easy to create a custom mapper that suites your specific needs.
<?php
use Zend\Diactoros\Response;
use Lucid\Mux\Request\ResponseMapperInterface.php;
class PsrResponseMapper implements ResponseMapperInterface
{
public function mapResponse($response)
{
return new Response($response);
}
}