dev-0.2-dev
dev-0.2-dev
The Development Requires
by Mike Timms
Wallogit.com
2017 © Pedro Peláez
Junction is a simple PHP router. Right now it's created as a personal project out of interest, but I plan to use it as the default router in my micro-RESTful PHP framework Planck., (*1)
Comments and suggestions welcome. This open source, so get forking and PRing if you want! :), (*2)
Junction comes with a ~~complete~~ set of tests., (*3)
You can run them with PHPUnit quite simply from your shell:, (*4)
vendor/bin/phpunit
Let's define a simple route, with only one path segment, (*5)
// define the route
$this->router->add('GET /hello', function () {
// just return a string for the application to then work on
return 'Hello, world';
});
// ... more routes ...
// handle the request, matching routes if possible
$response = $this->router->handleRequest();
The first argument to the add() funciton is a string which starts with the HTTP verb to match on, followed by the path requested., (*6)
Junction allows you to simply define variables in your route, using a similar placeholder syntax to PDO queries on your DB, (*7)
// define the route - with required variable "name"
$this->router->add('GET /hello/:name', function ($name) {
// just return a string for the application to then work on
return 'Hello, ' . $name;
});
// ... more routes ...
// handle the request, matching routes if possible
$response = $this->router->handleRequest();
The variable "name" is passed in as the first argument in your handling function., (*8)
// define the route - with optional variable "name"
$this->router->add('GET /hello/:name?', function ($name) {
// just return a string for the application to then work on
$name = isset($name) ? $name : 'world';
return 'Hello, ' . $name;
});
// ... more routes ...
// handle the request, matching routes if possible
$response = $this->router->handleRequest();
If the "name" is given then we can use it, but if it's omitted the route will still match and we can fall back on the string "world"., (*9)
You can validate the path variables in your route by providing a list of validation callbacks, (*10)
$this->router->add('GET /hello/:name', [
'name' => [
function ($value) {
// only accept short names
return strlen($value) < 5;
},
],
], function ($name) {
return 'Hello, ' . $name;
});
Validation errors will raise an Exception detailing the variable that failed validation and the invalid value., (*11)