lucid/mux

Psr7 compatible http Routing Library

v0.0.1 2016-04-12 19:51 UTC

This package is not auto-updated.

Last update: 2024-12-21 19:33:30 UTC


README

Author Source Code Software License

Build Status Code Coverage HHVM

Installation

> composer require lucid/mux

Usage

Creating coute collections

Manualy

<?php

use Lucid\Mux\Route;
use Lucid\Mux\Routes;

$routes = new Routes;
$routes->add('index', new Route('/', 'Acme\FrontController@getIndex'));

Using the Builder

<?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');

Dispatching routes

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);

Working with PSR-7 requests

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);

Dispatching named routes

<?php

$options = [
    'id' => 12
];

$response = $router->route('user.delete', $options);

Advanced router configuration

The router mostly relies on two main components:

  1. 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

The handler dispatcher

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.

Dependency Injection

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);

The response mapper

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);
    }
}