2017 © Pedro Peláez
 

library router

PHP request route

image

isholao/router

PHP request route

  • Tuesday, November 21, 2017
  • by isholao
  • Repository
  • 0 Watchers
  • 0 Stars
  • 13 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Build Status, (*1)

Install

To install with composer:, (*2)

composer require isholao/router

Requires PHP 7.1 or newer., (*3)

Usage

Here's a basic usage example:, (*4)

<?php

require '/path/to/vendor/autoload.php';

$c = \Isholao\Router\RouteCollection();
$c->get('/','get_all_users_responder');
$c->mapMany('GET|POST','/login','login_responder','login');
$c->disptach('GET','/login'); //return Route instance or null

Defining routes

$c = \Isholao\Router\RouteCollection();
$c->mapOne('GET', '/login', 'defined_responder');
$c->mapOne('GET', '/{lang=(?:en|de)}/login', 'defined_responder');
or
$c->mapMany('GET|POST', '/login, 'defined_responder');

You can also make an optional path segment by add a ? after the defined segement., (*5)

$c = \Isholao\Router\RouteCollection();
$c->mapOne('GET', '/something/{hash=[a-zA-Z]+}?', 'defined_responder');
or
$c->mapMany('GET|POST', '/login, 'defined_responder');

You can set a default value for a segment because each defined route returns a \Router\Route instance. Note this can only be done on mapOne method or any of the helper functions get(), post(), head(), delete(), option(), put(), (*6)

$c = \Isholao\Router\RouteCollection();
$c->mapOne('GET', '/something/{hash=[a-zA-Z]+}', 'defined_responder')->setParam('hash','asd8asdasd9');
or
$c->delete('/something/{hash=[a-zA-Z]+}', 'defined_responder')->setParam('hash','asd8asdasd9');

Route Groups

Additionally, you can specify routes inside of a group. All routes defined inside a group will have a common prefix., (*7)

For example, defining your routes as:, (*8)

$c = \Isholao\Router\RouteCollection();
$c->groupRoutes('/admin', function (\Isholao\Router\RouteCollectionInterface $r) {
    $r->mapOne('GET', '/do-something', 'handler'); // this becomes `/admin/do-something`
    $r->post('/do-something-else', function(){}); //  // this becomes `/admin/do-another-thing`
});

Dispatching a URI

A URI is dispatched by calling the dispatch() method of the created \Isholao\Router\RouteCollection object. This method accepts the HTTP method and a URI., (*9)

$c = \Isholao\Router\RouteCollection();
$c->groupRoutes('/admin', function (\Isholao\Router\RouteCollectionInterface $r) {
    $r->mapOne('GET', '/do-something', 'handler'); // this becomes `/admin/do-something`
    $r->post('/do-something-else', function(){}); //  // this becomes `/admin/do-another-thing`
});

$c->dispatch('GET','/admin/do-something'); // return a \Isholao\Router\Route instance or null

The Versions

21/11 2017

1.x-dev

1.9999999.9999999.9999999-dev https://github.com/isholao/router

PHP request route

  Sources   Download

BSD-3-Clause

The Requires

  • php >=7.1.0

 

The Development Requires

by Ishola Ogunseye

routing router

21/11 2017

1.0.1

1.0.1.0 https://github.com/isholao/router

PHP request route

  Sources   Download

BSD-3-Clause

The Requires

  • php >=7.1.0

 

The Development Requires

by Ishola Ogunseye

routing router

21/11 2017

1.0.0

1.0.0.0 https://github.com/isholao/router

PHP request route

  Sources   Download

BSD-3-Clause

The Requires

  • php >=7.1.0

 

The Development Requires

by Ishola Ogunseye

routing router