2017 © Pedro Peláez
 

library router

Respond to any requested URI in your application through a single point of entry.

image

p810/router

Respond to any requested URI in your application through a single point of entry.

  • Tuesday, July 24, 2018
  • by p810
  • Repository
  • 1 Watchers
  • 0 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 67 % Grown

The README.md

Router

Respond to any requested URI in your application through a single point of entry., (*1)

Installation

composer require p810/router

Usage

A route is any URI that you wish for your application to respond to. Routes must be contained within an instance of RouteCollection. The route may be checked against RouteCollection::match(), which will either throw an exception (UnmatchedRouteException) or call the specified callback., (*2)

Note that in order to use this, you must configure your Web server to direct requests to the script where this code is placed (e.g., .htaccess for Apache)., (*3)

Loading controllers

You may set a default namespace from which to load classes by calling Collection::setControllerNamespace(). Just tell it which namespace is the base of your controllers., (*4)

$router->setControllerNamespace('MyApp\\Controllers\\');

Dynamic routes

You may pass arguments into your route by using one of the following tokens., (*5)

Token Match
{int} Any integer (0-9).
{word} A word (a-zA-Z 0-9).

You may specify that a token is optional by prefixing the closing brace with a question mark, like so: {token?}, (*6)

Example

<?php

require_once dirname(__FILE__) . '/vendor/autoload.php';

/**
 * Create a collection and register a route to it.
 */

use p810\Router\UnmatchedRouteException;
use p810\Router\Collection as RouteCollection;

$router = new RouteCollection;

$router->register('/', function () {
    return 'Hello world!'; 
});

$router->register('/{word}', function ($name) {
    return sprintf('Hello %s, how are you today?', $name); 
});


/**
 * Attempt to match the route based on the current URI.
 *
 * The returned result will then be output.
 */

try {
    $result = $router->match( $_SERVER['REQUEST_URI'] );
} catch (UnmatchedRouteException $e) {
    http_response_code(404);

    $result = 'The requested resource could not be found!';
}

print $result;

The Versions

24/07 2018

2.x-dev

2.9999999.9999999.9999999-dev

Respond to any requested URI in your application through a single point of entry.

  Sources   Download

MIT

The Requires

 

by Payton

20/07 2018

dev-master

9999999-dev

Respond to any requested URI in your application through a single point of entry.

  Sources   Download

MIT

The Requires

 

by Payton

20/07 2018

1.1.2

1.1.2.0

Respond to any requested URI in your application through a single point of entry.

  Sources   Download

MIT

The Requires

 

by Payton

20/07 2018

1.1.1

1.1.1.0

Respond to any requested URI in your application through a single point of entry.

  Sources   Download

MIT

The Requires

 

by Payton

20/07 2018

1.1.0

1.1.0.0

Respond to any requested URI in your application through a single point of entry.

  Sources   Download

MIT

by Payton

21/12 2016

dev-development

dev-development

Respond to any requested URI in your application through a single point of entry.

  Sources   Download

MIT

by Payton

24/03 2016

v1.0.1

1.0.1.0

Respond to any requested URI in your application through a single point of entry.

  Sources   Download

MIT

by Payton

24/03 2016

v1.0.0

1.0.0.0

Respond to any requested URI in your application through a single point of entry.

  Sources   Download

MIT

by Payton