2017 © Pedro Peláez
 

library php_router

Simple routing engine, which allows anonymous functions to execute code on route.

image

plugowski/php_router

Simple routing engine, which allows anonymous functions to execute code on route.

  • Monday, February 22, 2016
  • by plugowski
  • Repository
  • 2 Watchers
  • 2 Stars
  • 43 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 5 % Grown

The README.md

PhpRouter PhpRouter alt text alt text alt text

PHP router inspired on FatFree, CakePHP and own invention., (*1)

It allows to use anonymous functions as callback as well as OOP classes and methods. In addition, it detects if request is asynchronous or not (defined by [ajax] tag in rute definition)., (*2)

Installation

Just clone that repository or use composer:, (*3)

composer require plugowski/php_router

Next in your project create .htaccess file which point on your dispatcher file:, (*4)

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule .* index.php?url=$1 [QSA,L]
</IfModule>

Usage

Basic usage looks like code below:, (*5)

<?php
require __DIR__ . '/vendor/autoload.php';

use PhpRouter\Route;
use PhpRouter\RouteCollection;
use PhpRouter\Router;
use PhpRouter\RouteRequest;

$routing = new RouteCollection();
$routing->attach(new Route('GET /', function(){
    echo 'Hello World';
}));

(new Router(new RouteRequest(), $routing))->run();

If you want to trigger OOP classes and methods, just write full namespace and method reference (-> or ::):, (*6)

new Route('GET /', '\Full\Namespace\Class->method');

For named parameters use @name tag, for example:, (*7)

new Route('GET /order/@id', '\Full\Namespace\Class->method');

Standard all named parameters will be recognized as (\w-)+, but if you want to define your own pattern, do it in second argument as associative array, where keys match to @ tag:, (*8)

// $_SERVER['REQUEST_URI'] = '/order/14-XA-43321'
new Route('GET /order/@id', ['id' => '\d{2}\-\w{2}\-\d{5}'], function($params) {
  echo 'Order ID: ' . $params['id']; // Order ID: 14-XA-43321 
});

All named params are passed into called function as associative array in first argument., (*9)

Licence

New BSD Licence: https://opensource.org/licenses/BSD-3-Clause, (*10)

The Versions

22/02 2016

dev-master

9999999-dev

Simple routing engine, which allows anonymous functions to execute code on route.

  Sources   Download

New BSD

The Development Requires

by Paweł Ługowski

22/02 2016

1.0

1.0.0.0

Simple routing engine, which allows anonymous functions to execute code on route.

  Sources   Download

New BSD

The Development Requires

by Paweł Ługowski