2017 © Pedro Peláez
 

library router

Lightweight URL Router

image

objectiveweb/router

Lightweight URL Router

  • Monday, February 12, 2018
  • by guigouz
  • Repository
  • 1 Watchers
  • 1 Stars
  • 814 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 19 Versions
  • 0 % Grown

The README.md

Objectiveweb URL Router Build Status

Lightweight url router with dependency injection support., (*1)

Instalation

Add the dependency to composer.json, then composer install, (*2)

{
    "require": {
        "objectiveweb/router": "~2.0"
    }
}

Basic Usage

In Objectiveweb applications, an endpoint refers to a php file which responds to url routes., (*3)

Under the hood, the route($regex, $callable) function tests $regex against the current request method and uri (e.g. "GET endpoint.php/something/([0-9]+)/?"), passing the captured parameters to $callable when matched., (*4)

Example endpoint

<?php
// include the composer autoloader
require_once('../vendor/autoload.php')

use Objectiveweb\Router;

$app = new Router();

// Routes are matched in order

$app->GET('/?', function() {
    echo "Hello index';
});

$app->GET('/([a-z]+)/?', function($key) {

    switch($key) {
        case 'data':
            // If the callback returns something, it's sent with status 200 (OK)
            // Arrays are automatically encoded to json
            return [ 1, 2, 3 ];
            break;
        default:
            throw new \Exception([ 'key' => $key, 'empty' => true ], 404); // respond with custom code
            break;
    }
});

$app->POST('/panic', function() {
    // Exceptions are captured, message is send with the proper code
    throw new \Exception('Panic!', 500);
});

// catch all
$app->router("([A_Z]+) (.*), function ($method, $path) {
    return "Request matched $method and $path";
});
  • GET http://server/endpoint.php displays Hello index
  • GET http://server/endpoint.php/data returns a json-encoded array with values 1, 2, 3
  • GET http://server/endpoint.php/test returns a not found error with a json object on its body
  • POST http://server/endpoint.php/panic raises a 500 Internal server error with 'Panic!' as the response

Controllers

PHP classes may be bound to an url using the controller($path, $class) on public-facing endpoint (index.php), (*5)

$app->controller('/', 'ExampleController');

In this case, the request is mapped to the corresponding class method as follows, (*6)

<?php

class ExampleController {

    // GET /?k=v
    function index($querystring) {

    }

    // GET /(.*)?k=v
    function get($path1, $path2, ..., $querystring) {

    }

    // Runs before all actions
    function before() {

    }

    // Runs before post();
    function beforePost($body) {

    }

    // POST /
    function post($body) {

    }

    // Other request methods are also valid, i.e. head(), options(), beforeHead(), etc

}

When a function named like the first parameter ($path[0]) exists on the controller, it gets called with the remaining parameters, (*7)

// (GET|POST|PUT|...) /example/(.*)
function example($path[1], $path[2], ...) {
    // check $_SERVER['REQUEST_METHOD'] and process data
}

This function name may also be prefixed with the request method. In this case the query parameters are passed as the last argument, (*8)

// GET /example/(.*)
function getExample($path[1], $path[2], ..., $_GET) {

}

// POST /example/(.*)
function postExample($path[1], $path[2], ..., $decoded_post_body) {

}

Other request methods are also valid (i.e. HEAD, OPTIONS, etc), check the example subdir for other uses., (*9)

Automatic routing

You can bootstrap the application on a particular namespace using, (*10)

$app->run('Namespace');

When run, the Router automatically maps the incoming requests to the given namespace. For example, a request to /products would instantiate the Namespace\ProductsController class., (*11)

If the Controller doesn't exist, the request is passed to the Namespace\HomeController. Check example/app-run.php for a working demo., (*12)

Dependency Injection

Since version 2.0, the Router extends Dice, which provides a dependency injection container to the application., (*13)

<?php
// include the composer autoloader
require_once('../vendor/autoload.php')

use Objectiveweb\Router;

$app = new Router();

// Configure the DI container rules for the PDO class
$app->addRule('PDO', [
    'shared' => true,
    'constructParams' => ['mysql:host=127.0.0.1;dbname=mydb', 'username', 'password'] 
]);

// From now on, you can get a configured PDO instance using
$pdo = $app->create('PDO');

When bound to paths, Controller (And dependencies of those dependencies) get automatically resolved. For example, if you define the controller, (*14)

<?php

namespace MyApplication;

class MyController {

    private $pdo;

    function __construct(PDO $pdo) {
        $this->pdo = pdo;
    }

    function index() {
        // query the database using $this->pdo
    }
}

When MyApplication\MyController gets instantiated by the Router, a configured instance of PDO will be injected and reused as necessary., (*15)

You can inject dependencies adding type-hinted parameters to your controller's constructor:, (*16)

function __construct(\Util\Gmaps $gmaps, \DB\ProductsRepository $products) {
  $this->gmaps = $gmaps;
  $this->products = $products;
}

// Use $this->gmaps and $this->products on other functions

In a another example, let's instantiate Twig, (*17)

// index.php

$app = new \Objectiveweb\Router();

$app->addRule('Twig_Loader_Filesystem', array(
    'shared' => true,
    'constructParams' => [ TEMPLATE_ROOT ]
));

$app->addRule('Twig_Environment', array(
    'shared' => true,
    'constructParams' => [
        [ 'instance' => 'Twig_Loader_Filesystem' ],
        [ 'cache' => APP_ROOT.'/cache' ],
        [ 'auto_reload' => true ]
    ],
    'call' => [
        [ 'addGlobal', [ 'server', $_SERVER['SERVER_NAME'] ] ],
        [ 'addGlobal', [ 'app_name', APP_NAME ] ],
        [ 'addGlobal', [ 'session', $_SESSION ] ],
        [ 'addFunction', [ new Twig_SimpleFunction('url', function ($path) {
            return \Objectiveweb\Router::url($path);
        })]]
    ]
));

$app->controller('/', 'MyController')

Then, inject it on your controller's constructor, (*18)

class MyController {

    private $twig;
    private $pdo;

    function __construct(Twig_Environment $twig, PDO $pdo) {
        $this->twig = $twig;
        $this->pdo = $pdo;
    }

    function index() {
        return $this->twig->render(...);
    }
}

You can also fetch the twig reference using, (*19)

$twig = $app->create('Twig_Environment');

Rules

Dice Rules can be configured with these properties:, (*20)

  • shared (boolean) - Whether a single instance is used throughout the container. View Example
  • inherit (boolean) - Whether the rule will also apply to subclasses (defaults to true). View Example
  • constructParams (array) - Additional parameters passed to the constructor. View Example
  • substitutions (array) - key->value substitutions for dependencies. View Example
  • call (multidimensional array) - A list of methods and their arguments which will be called after the object has been constructed. View Example
  • instanceOf (string) - The name of the class to initiate. Used when the class name is not passed to $app->addRule(). View Example
  • shareInstances (array) - A list of class names that will be shared throughout a single object tree. View Example

The Versions

12/02 2018

dev-master

9999999-dev

Lightweight URL Router

  Sources   Download

MIT

The Requires

 

The Development Requires

12/02 2018

v2.0.10

2.0.10.0

Lightweight URL Router

  Sources   Download

MIT

The Requires

 

The Development Requires

01/03 2016

v2.0.9

2.0.9.0

Lightweight URL Router

  Sources   Download

MIT

The Requires

 

The Development Requires

26/02 2016

v2.0.8

2.0.8.0

Lightweight URL Router

  Sources   Download

MIT

The Requires

 

The Development Requires

25/02 2016

v2.0.7

2.0.7.0

Lightweight URL Router

  Sources   Download

MIT

The Requires

 

The Development Requires

25/02 2016

v2.0.6

2.0.6.0

Lightweight URL Router

  Sources   Download

MIT

The Requires

 

The Development Requires

24/02 2016

v2.0.4

2.0.4.0

Lightweight URL Router

  Sources   Download

MIT

The Requires

 

The Development Requires

24/02 2016

v2.0.3

2.0.3.0

Lightweight URL Router

  Sources   Download

MIT

The Requires

 

The Development Requires

19/01 2016

v2.0.2

2.0.2.0

Lightweight URL Router

  Sources   Download

MIT

The Requires

 

The Development Requires

19/01 2016

v2.0.5

2.0.5.0

Lightweight URL Router

  Sources   Download

MIT

The Requires

 

The Development Requires

17/01 2016

v2.0.1

2.0.1.0

Lightweight URL Router

  Sources   Download

MIT

The Requires

 

The Development Requires

17/01 2016

v1.3.1

1.3.1.0

Lightweight URL Router

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

17/01 2016

v2.0.0

2.0.0.0

Lightweight URL Router

  Sources   Download

MIT

The Requires

 

The Development Requires

16/01 2016

v1.3.0

1.3.0.0

Lightweight URL Router

  Sources   Download

MIT

The Development Requires

06/05 2015

v1.2.1

1.2.1.0

Objectiveweb's URL Router

  Sources   Download

MIT

12/04 2015

v1.2.0

1.2.0.0

Objectiveweb's URL Router

  Sources   Download

MIT

19/12 2014

v1.1.0

1.1.0.0

Objectiveweb's URL Router

  Sources   Download

MIT

09/06 2014

v1.0.1

1.0.1.0

Objectiveweb's URL Router

  Sources   Download

MIT

20/05 2014

v1.0.0

1.0.0.0

Objectiveweb's URL Router

  Sources   Download

MIT