2017 © Pedro Peláez
 

library mux

Psr7 compatible http Routing Library

image

lucid/mux

Psr7 compatible http Routing Library

  • Sunday, October 30, 2016
  • by iwyg
  • Repository
  • 1 Watchers
  • 0 Stars
  • 100 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

A PSR-7 compatible HTTP router

Author Source Code Software License, (*1)

Build Status Code Coverage HHVM, (*2)

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., (*3)

<?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., (*4)

<?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:, (*5)

  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, (*6)

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., (*7)

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., (*8)

<?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., (*9)

<?php

use Zend\Diactoros\Response;

use Lucid\Mux\Request\ResponseMapperInterface.php;

class PsrResponseMapper implements ResponseMapperInterface
{
    public function mapResponse($response)
    {
        return new Response($response);
    }
}

The Versions

30/10 2016

dev-develop

dev-develop

Psr7 compatible http Routing Library

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar iwyg

http routing prs7

12/04 2016

dev-master

9999999-dev

Psr7 compatible http Routing Library

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

by Avatar iwyg

http routing prs7

12/04 2016

v0.0.1

0.0.1.0

Psr7 compatible http Routing Library

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

by Avatar iwyg

http routing prs7