dev-master
9999999-devA small router library with path variable support.
MIT
The Requires
- php >=5.4.0
The Development Requires
route router
A small router library with path variable support.
A small router with path variable support., (*1)
Routes, (*2)
$routes = [ '/home' => [ 'function' => 'main' ] ];
PHP, (*3)
<?php use BellevueRouter\src\Router; require_once 'routes.php'; $router = new Router($routes); $router->route('/home'); function main() { echo 'Hello!'; }
Routes, (*4)
$routes = [ '/home' => [ 'class' => 'StaticRoutes', 'method' => 'main' ] ];
PHP, (*5)
<?php use BellevueRouter\src\Router; require_once 'routes.php'; $router = new Router($routes); $router->route('/home'); class StaticRoutes { public static function main() { echo 'Static class method.' } }
Routes, (*6)
$routes = [ '/home' => [ 'object' => 'Routes', 'method' => 'sayHi' ] ];
PHP, (*7)
<?php use BellevueRouter\src\Router; require_once 'routes.php'; $router = new Router($routes); $router->route('/home'); class Routes { public static function sayHi() { echo 'Object instance method.' } }
Arguments to be passed to the function are given in the arguments array. They will be passed to the function in the same order as defined here, so if your function definition looks like myFunc($var1, $var2)
and your arguments array is ['val1, 'val2']
, myFunc will recieve those values in the $var1 and $var2 variables., (*8)
You can also inject path values into your function via named sections of your path. That is, if you have a path like /post/{id}
, the router will look in your arguments array for a string with the same name: {id}
, and replace it with the value the user puts in the path. So if the user hits path /post/12
, and you have your arguments array like ['{id}']
, the string "12" would be passed as the first function argument., (*9)
The example below show full argument usage., (*10)
Routes, (*11)
$clientIp = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'unknown'; $routes = [ '/hi/{name}' => [ 'function' => 'hi', 'arguments' => ['{name}', $clientIp] ] ];
PHP, (*12)
<?php use BellevueRouter\src\Router; require_once 'routes.php'; $router = new Router($routes); $router->route('/hi/Yoshi'); function hi($name, $ip) // variables can have any name here. { printf('Hello, %s! Your IP address is %s', $name, $ip); }
Note:, (*13)
The router does not perform an path variable sanitization. If a variable is specified, every character up to the next forward slash or end of string is captured and passed to your function., (*14)
There is also an optional default route that can be called when no path is given, or when no path is matched., (*15)
Routes, (*16)
$clientIp = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'unknown'; $routes = [ 'default' => [ 'function' => 'home', ] ];
PHP, (*17)
<?php use BellevueRouter\src\Router; require_once 'routes.php'; $router = new Router($routes); $router->route('unmatched'); function home() { echo 'Welcome home!'; }
A small router library with path variable support.
MIT
route router