2017 © Pedro Peláez
 

library router

Anax Router module.

image

anax/router

Anax Router module.

  • Friday, March 16, 2018
  • by mikael_roos
  • Repository
  • 1 Watchers
  • 1 Stars
  • 3,123 Installations
  • PHP
  • 21 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 18 Versions
  • 9 % Grown

The README.md

Anax Router

Latest Stable Version Join the chat at https://gitter.im/canax/router, (*1)

Build Status CircleCI, (*2)

Build Status Scrutinizer Code Quality Code Coverage, (*3)

Maintainability Codacy Badge, (*4)

Anax Router module., (*5)

A standalone router supporting request methods and dynamic routes matching, extracting and validating arguments from path., (*6)

The router will try matching routes by the order they were added and execute all matching routes, one after the other., (*7)

Use exit() to prevent further routes from being matched., (*8)

Install

$ composer require anax/router

Usage

Add some routes with handlers

use Anax\Route\Router;

$router = new Router();

$router->add("", function () {
    echo "home ";
});

$router->add("about", function () {
    echo "about ";
});

$router->add("about/me", function () {
    echo "about/me ";
});

// try it out
$router->handle("");
$router->handle("about");
$router->handle("about/me");
// home about about/me

Add multiple routes with one handler

Add multiple routes, through an array of rules, sharing a handler., (*9)

$router = new Router();

$router->add(["info", "about"], function () {
    echo "info or about - ";
});

// try it out
$router->handle("info");
$router->handle("about");
// info or about - info or about -

Add a default route

This route will match any path., (*10)

$router = new Router();

$router->always(function () {
    echo "always ";
});

// try it out using some paths
$router->handle("info");
$router->handle("about");
// always always

Add internal routes for 404, 403 and 500 error handling

Add an internal route that is called when no route can be matched., (*11)

$router = new Router();

$router->addInternal("404", function () {
    echo "404 ";
});

$router->add("about", function () {
    echo "about ";
});

// try it out using some paths
$router->handle("whatever");
// 404

You can add internal routes to deal with 403 and 500. These routes will handle uncaught exceptions thrown within a route handler., (*12)

The 403 internal route that is catching exception of type ForbiddenException., (*13)

$router->addInternal("403", function () {
    echo "403 ";
});

$router->add("login", function () {
    throw new ForbiddenException();
});

// try it out using some paths
$router->handle("login");
// 403

The 500 internal route that is catching exception of type InternalErrorException., (*14)

$router->addInternal("500", function () {
    echo "500 ";
});

$router->add("calculate", function () {
    throw new InternalErrorException();
});

// try it out using some paths
$router->handle("calculate");
// 500

Add a common route for any item below subpath using *

This route will match any item on the same level as about/*., (*15)

$router = new Router();

$router->addInternal("404", function () {
    echo "404 ";
});

$router->add("about/*", function () {
    echo "about ";
});

// try it out using some paths
$router->handle("about");
$router->handle("about/me");
$router->handle("about/you");
$router->handle("about/some/other"); // no match
// about about about 404

Add a common route for any item below subpath using **

This route will match any item below about/**, even subdirs., (*16)

$router = new Router();

$router->add("about/**", function () {
    echo "about ";
});

// try it out using some paths
$router->handle("about");
$router->handle("about/me");
$router->handle("about/you");
$router->handle("about/some/other");
// about about about about

Part of path as arguments to the route handler

You can send a part of the route as an argument to the handler. This makes a route handler more flexible and dynamic., (*17)

$router = new Router();

$router->addInternal("404", function () {
    echo "404 ";
});

$router->add("about/{arg}", function ($arg) {
    echo "$arg ";
});

ob_start();
// try it out using some paths
$router->handle("about");            // not matched
$router->handle("about/me");
$router->handle("about/you");
$router->handle("about/some/other"); // not matched
// 404 me you 404

You can send multiple arguments., (*18)

$router = new Router();

$router->add(
    "post/{year}/{month}/{day}",
    function ($year, $month, $day) {
        echo "$year-$month-$day, ";
    }
);

// try it out using some paths
$router->handle("post/2017/03/07");
$router->handle("post/1990/06/20");
// 2017-03-07, 1990-06-20,

Type checking of arguments

Apply type checking to the arguments to restrict a the routes being matched., (*19)

$router = new Router();

$router->addInternal("404", function () {
    echo "404, ";
});

$router->add(
    "post/{year:digit}/{month:digit}/{day:digit}",
    function ($year, $month, $day) {
        echo "$year-$month-$day, ";
    }
);

$router->add(
    "post/{year:digit}/{month:alpha}/{day:digit}",
    function ($year, $month, $day) {
        echo "$day $month $year, ";
    }
);

// try it out using some paths
$router->handle("post/2017/03/seven");
$router->handle("post/2017/03/07");
$router->handle("post/1990/06/20");
$router->handle("post/1990/june/20");
// 404, 2017-03-07, 1990-06-20, 20 june 1990,

For type checking is digit, alpha, alphanum and hex supported (see ctype for details)., (*20)

Routes per request method

A route can be setup to match only one request method., (*21)

$router = new Router();

$router->any(["GET"], "about", function () {
    echo "GET ";
});

$router->any(["POST"], "about", function () {
    echo "POST ";
});

$router->any(["PUT"], "about", function () {
    echo "PUT ";
});

$router->any(["DELETE"], "about", function () {
    echo "DELETE ";
});

// try it out using some paths
$router->handle("about", "GET");
$router->handle("about", "POST");
$router->handle("about", "PUT");
$router->handle("about", "DELETE");
// GET POST PUT DELETE

A route can also match several request methods., (*22)

$router = new Router();

$router->any(["GET", "POST"], "about", function () {
    echo "GET+POST ";
});

$router->any("PUT | DELETE", "about", function () {
    echo "PUT+DELETE ";
});

// try it out using some paths
$router->handle("about", "GET");
$router->handle("about", "POST");
$router->handle("about", "PUT");
$router->handle("about", "DELETE");
// GET+POST GET+POST PUT+DELETE PUT+DELETE

Dependency

These are the dependencies to other modules., (*23)

Module What
anax/commons Using Anax\Commons\ContainerInjectableInterface.
anax/commons Using Anax\Commons\ContainerInjectableTrait.

License

This software carries a MIT license. See LICENSE.txt for details., (*24)

 .  
..:  Copyright (c) 2013 - 2019 Mikael Roos, mos@dbwebb.se

The Versions

16/03 2018

dev-master

9999999-dev https://dbwebb.se/anax

Anax Router module.

  Sources   Download

MIT

The Requires

 

The Development Requires

  • php >=5.6

micro framework boilerplate mvc education

16/03 2018

v1.1.0

1.1.0.0 https://dbwebb.se/anax

Anax Router module.

  Sources   Download

MIT

The Requires

 

micro framework boilerplate mvc education

28/09 2017

v1.0.15

1.0.15.0 https://dbwebb.se/anax

Anax Router module.

  Sources   Download

MIT

The Requires

 

micro framework boilerplate mvc education

26/09 2017

v1.0.14

1.0.14.0 https://dbwebb.se/anax

Anax Router module.

  Sources   Download

MIT

The Requires

 

micro framework boilerplate mvc education

14/09 2017

v1.0.13

1.0.13.0 https://dbwebb.se/anax

Anax Router module.

  Sources   Download

MIT

The Requires

 

micro framework boilerplate mvc education

15/08 2017

v1.0.12

1.0.12.0 https://dbwebb.se/anax

Anax Router module.

  Sources   Download

MIT

The Requires

 

micro framework boilerplate mvc education

10/08 2017

v1.0.11

1.0.11.0 https://dbwebb.se/anax

Anax Router module.

  Sources   Download

MIT

The Requires

 

micro framework boilerplate mvc education

10/08 2017

v1.0.10

1.0.10.0 https://dbwebb.se/anax

Anax Router module.

  Sources   Download

MIT

The Requires

 

micro framework boilerplate mvc education

03/08 2017

v1.0.9

1.0.9.0 https://dbwebb.se/anax

Anax Router module.

  Sources   Download

MIT

The Requires

 

micro framework boilerplate mvc education

27/06 2017

v1.0.8

1.0.8.0 https://dbwebb.se/anax

Anax Router module.

  Sources   Download

MIT

The Requires

 

micro framework boilerplate mvc education

27/06 2017

v1.0.7

1.0.7.0 https://dbwebb.se/anax

Anax Router module.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

micro framework boilerplate mvc education

27/06 2017

v1.0.6

1.0.6.0 https://dbwebb.se/anax

Anax Router module.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

micro framework boilerplate mvc education

24/04 2017

v1.0.5

1.0.5.0 https://dbwebb.se/anax

Anax Router module.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

micro framework boilerplate mvc education

13/04 2017

v1.0.4

1.0.4.0 https://dbwebb.se/anax

Anax Router module.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

micro framework boilerplate mvc education

26/03 2017

v1.0.3

1.0.3.0 https://dbwebb.se/anax

Anax Router module.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

  • php >=5.6

micro framework boilerplate mvc education

26/03 2017

v1.0.2

1.0.2.0 https://dbwebb.se/anax

Anax Router module.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

  • php >=5.6

micro framework boilerplate mvc education

13/03 2017

v1.0.1

1.0.1.0 https://dbwebb.se/anax

Anax Router module.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

  • php >=5.6

micro framework boilerplate mvc education

07/03 2017

v1.0.0

1.0.0.0 https://dbwebb.se/anax

Anax Router module.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

  • php >=5.6

micro framework boilerplate mvc education