2017 © Pedro Peláez
 

library router

A simple PHP Router handling complex route patterns

image

noa/router

A simple PHP Router handling complex route patterns

  • Tuesday, September 19, 2017
  • by Akanoa
  • Repository
  • 1 Watchers
  • 0 Stars
  • 16 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

Build Status codecov, (*1)

Yet another Router

This a simple PHP Router handling complex route patterns, (*2)

Description

Provides a simple way to handle parametrized routes, compliant PSR-7., (*3)

Installation

composer require noa/router

Usages

How to create a Router

Router handles routes, you must create a router before adding route., (*4)

<?php
require_once vendor/autoload.php

use Noa\Router\Router;

$router = new Router();

Get router instance

You can also get a Router instance through a static call, with or without custom configuration, (*5)

$router = Router::getInstance();

When you want to destroy this instance to create a new one, just, (*6)

Router::destroy();

How to define routes

A route is a group of three properties: - The HTTP verb matching with route - The path pattern of the route - The function to call in case of match, (*7)

You can define routes as many as you want., (*8)

For example purpose, here is a DummyController, (*9)

namespace Noa\Router\Example;


class DummyController
{
    public function testGet() {
         return 'success Get';
    }

    public function testPut() {
        return 'success Put';
    }

    public function testPost() {
        return 'success Post';
    }

    public function testDelete() {
        return 'success Delete';
    }

    public function testWithParameter($param) {

        return 'success '.$param;
    }

    public function testWithMoreParameter($param, $param2) {

        return 'success '.$param.':'.$param2;
    }

    public function testWithMoreParameterConstraint($param, $param2) {

        return 'success constraint '.$param.':'.$param2;
    }
}

Simple GET route, (*10)

This the simpler route possible, (*11)

$router->get('/test/closure', function (){
    return 'success closure';
});

Controller, (*12)

Out of closure, you can use class method as controller, (*13)

The callable must follow this pattern:, (*14)

\Namespace\Of\Class\ClassName#method

Pattern matching following HTTP verb, (*15)

A pattern could match multiple HTTP verb, of course you can associate the same controller to all of them., (*16)

$router->get('/object', 'Noa\Router\Example\DummyController#testGet');
$router->put('/object', 'Noa\Router\Example\DummyController#testPut');
$router->post('/object', 'Noa\Router\Example\DummyController#testPost');
$router->delete('/object', 'Noa\Router\Example\DummyController#testDelete');

Parametrized route, (*17)

You can parametrized by adding a semi-colon before route part. Thus all url like: - /test/test - /test/12 - /test/whatever, (*18)

Will match this route:, (*19)

$router->get('/object/:param', 'Noa\Router\Example\DummyController#testWithParameter');

The same thing could be achieve with closure., (*20)

$router->get('/object/closure/:param', function ($param){
    return 'success closure '.$param;
});

Constraints on parameter, (*21)

Sometimes you want to match a route only if parameter match a specific regex, the with method allows to add constraints on parameter (route part beginning by ":"), (*22)

Those two routes have the same verb and the same pattern but are considered as different routes because constraint on :param2., (*23)

You can chain constraints as many as you want., (*24)

$router->get('/object/:id/:method/:param', 'Noa\Router\Example\DummyController#testWithMoreParameterConstraint')
    ->with('method', '[a-z]+')
    ->with('param', '[0-9]+')
    ->with('id', '[0-9]+');

$router->get('/object/:id/:method', 'Noa\Router\Example\DummyController#testWithMoreParameter');

Launch route matcher

The run method will call the route controller if the request URL and HTTP verb match with one the route, (*25)

The return of run method will the controller return, feel free to do what you want with the return., (*26)

In this example we only echoing this return., (*27)

If none of route matches, an exception is raised., (*28)

try {

    echo $router->run();

} catch (\Noa\Router\RouterException $e) {

    switch ($e->getCode()) {
        case \Noa\Router\RouterException::ROUTE_NOT_FOUND:
            // Some 404 page
            break;
        default:
            // Something else
            break;
    }
}

All this code is available into example folder., (*29)

You can use PHP intern server by, (*30)

cd example
php -S localhost:8082 -t .

Then you can request with curl or Postman to test route matching, (*31)

PSR-7, (*32)

This Router is PSR-7 compliant. It means that you receive an object Request as describe in PGP-FIG and return a Response also compliant PSR-7., (*33)

Two function are available which return the objects:, (*34)

The first one is read only and handle all parameter receive by server., (*35)

$request=Router::getRequest();

The second one allow to set data into response before return., (*36)

$response=Router::getResponse();

More information about here., (*37)

The Versions

19/09 2017

dev-master

9999999-dev

A simple PHP Router handling complex route patterns

  Sources   Download

BSD

The Requires

 

The Development Requires

by Yannick Guern

19/09 2017

1.1.1

1.1.1.0

A simple PHP Router handling complex route patterns

  Sources   Download

BSD

The Requires

 

The Development Requires

by Yannick Guern

19/09 2017

1.1.0

1.1.0.0

A simple PHP Router handling complex route patterns

  Sources   Download

BSD

The Requires

 

The Development Requires

by Yannick Guern

17/09 2017

1.0.4

1.0.4.0

A simple PHP Router handling complex route patterns

  Sources   Download

BSD

The Requires

  • php >5.5

 

The Development Requires

by Yannick Guern

17/09 2017

1.0.3

1.0.3.0

A simple PHP Router handling complex route patterns

  Sources   Download

BSD

The Requires

  • php >5.5

 

The Development Requires

by Yannick Guern

17/09 2017

1.0.2

1.0.2.0

A simple PHP Router handling complex route patterns

  Sources   Download

BSD

The Requires

  • php >5.5

 

The Development Requires

by Yannick Guern

17/09 2017

1.0.1

1.0.1.0

A simple PHP Router handling complex route patterns

  Sources   Download

BSD

The Requires

  • php >5.5

 

The Development Requires

by Yannick Guern

17/09 2017

1.0.0

1.0.0.0

A simple PHP Router handling complex route patterns

  Sources   Download

BSD

The Requires

  • php >5.5

 

The Development Requires

by Yannick Guern