Installing

Add "packfire/router": "1.0.*" under the "require" configuration in composer.json.

Run php composer.phar install to install Packfire Router.

Loading Routes from Config

Simply load your routes from any configuration format you like:

use Packfire\Router\FileLoader;
$loader = new FileLoader('config/routes.yml'); // loading from a YAML file
$loader = new FileLoader('config/routes.json'); // loading from a JSON file
$loader = new FileLoader('config/routes.php'); // or loading from a PHP file that returns an array of configuration
// create the Router object using the configuration
$router = $loader->load();

Routing Requests

Routing requests is simple:

use Packfire\Router\FileLoader;
use Packfire\Router\CurrentRequest;
$loader = new FileLoader('config/routes.yml');
$router = $loader->load();
// load data from $_SERVER
$request = new CurrentRequest();
// Get the route based on the current request
$route = $router->route($request);

With the $route object in hand, you can easily execute the $route by simply running the callback:

// performs the action that the route needs to
$route->callback();

Generating URLs from Routes

You can perform reverse engineering by putting the route and parameters to form its full working URL: