Skip to content

Latest commit

History

14 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

PhpRouter PhpRouter alt textalt textalt text

PHP router inspired on FatFree, CakePHP and own invention.

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).

Installation

Just clone that repository or use composer:

composer require plugowski/php_router

Next in your project create .htaccess file which point on your dispatcher file:

<IfModulemod_rewrite.c>
RewriteEngineonRewriteCond%{REQUEST_FILENAME}!-fRewriteCond%{REQUEST_FILENAME}!-dRewriteCond%{REQUEST_FILENAME}!-lRewriteRule.*index.php?url=$1 [QSA,L]
</IfModule>

Usage

Basic usage looks like code below:

<?phprequire__DIR__ . '/vendor/autoload.php';
usePhpRouter\Route;
usePhpRouter\RouteCollection;
usePhpRouter\Router;
usePhpRouter\RouteRequest;
$routing = newRouteCollection();
$routing->attach(newRoute('GET /', function(){
echo'Hello World';
}));
(newRouter(newRouteRequest(), $routing))->run();

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

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

For named parameters use @name tag, for example:

newRoute('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:

// $_SERVER['REQUEST_URI'] = '/order/14-XA-43321'newRoute('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.

Licence

New BSD Licence: https://opensource.org/licenses/BSD-3-Clause

About

Simple router, which allows you use anonymous functions and call object methods.

Resources

Stars

3 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages