Today I will giving a tutorial on how to use Cribz Router. Cribz Router is PHP 5 url routing library and is similar to the routing in Dancer.
Requirements:
- A webserver that is configured to run php
- A web config that rewrites all requests to index.php
First of all you can get Cribz Router from packagist, https://packagist.org/packages/cribz/router using composer.
First of all install composer:
$ curl -sS https://getcomposer.org/installer | php
Create a file called composer.json:
{
"require": {
"cribz/router": "v1.0.2"
}
}
Run composer.phar install This command reads the composer.json and downloads all the require libaries and put them into the vendor folder and creates a composer.lock file.
Here is a simple hello world using Cribz Router (index.php):
<?php
// Include the auto generated autoloader that is created via composer
require_once(__DIR__ . '/vendor/autoload.php');
use Cribz\RouterException;
use Cribz\Router;
try {
Router::get('/', function($request, $params) {
echo 'Hello world';
});
Router::run();
} catch (RouterException $e) {
echo 'Error: ' . $e->getMessage();
}
?>
Now if you hit the / in a browser it will show "Hello World".
Now lets do a simple post route:
<?php
// Include the auto generated autoloader that is created via composer
require_once(__DIR__ . '/vendor/autoload.php');
use Cribz\RouterException;
use Cribz\Router;
try {
// Handle HTTP GET request for /
Router::get('/', function($request, $params) {
echo 'Hello world';
});
// Handle HTTP GET request for /contact
Router::get('/contact', function($request, $params) {
echo '
<form action="/contact" method="post"><textarea name="message"></textarea><input type="submit" /></form>
';
});
// Handle HTTP POST request for /contact
Router::post('/contact', function($request, $params) {
// Get the user input from the params object
$message = $params->post->message;
echo 'Thank you for the message';
echo 'Message: ' . $message;
});
Router::run();
} catch (RouterException $e) {
echo 'Error: ' . $e->getMessage();
}
?>;
Now if you hit /contact you will be presented with a form, add a message then submit the form then it will present you with a thankyou message and the message you entered into the form.
Cribz Router is small but powerful library here is the full api:
Router::any(array $methods, string $uri, callback $function) Set a route for multiple HTTP request methods.
Router::delete(string $uri, callback $function) Set a route for a HTTP Delete request.
Router::exists(string $method, string $uri) Check if a route exists.
Router::get(string $uri, callback $function) Set a route for a HTTP Get request.
Router::head(string $uri, callback $function) Set a route for a HTTP Head request.
Router::options(string $uri, callback $function) Set a route for a HTTP Options request.
Router::post(string $uri, callback $function) Set a route for a HTTP Post request.
Router::put(string $uri, callback $function) Set a route for a HTTP Put request
Router::run() Run the routes. Wraps both runCli() & runHttp()
Router::runCli() Run routes from the Command Line.
Router::runHttp() Run routes from a HTTP request.
The libraries code is here: https://github.com/chtombleson/cribz-router