Hubert

Routing

Hubert comes with AltoRouter.

A Route

$config = array(
    "routes" => array(
        "hello" => array(
            "route" => "/hello[/]", 
            "method" => "GET"
            "target" => function($request, $response, $args){
                echo "Hello-Route";
            }
        )
    )
);

Every Route must have a unique name, in our example it's hello. A route consists of thee parts:

Route-Matching

Static routes are only executed when they match the request uri exactly:

$config = array(
    "routes" => array(
        "home" => array(
            "route" => "/", 
            "target" => function($request, $response, $args){
                echo "Hello World";
            }
        ),
        "hello" => array(
            "route" => "/hello", 
            "target" => function($request, $response, $args){
                echo "Hello-Route";
            }
        )
    )
);

The route "hello" only matches "/hello" but not "/hello/". If there is a slash at the end of the route you can use "route" => "/hello/" to create a matching route or simply use [/]? at the end to optionalise slashes:

$config = array(
    "routes" => array(
        "hello" => array(
            "route" => "/hello[/]?", 
            "target" => function($request, $response, $args){
                echo "Hello-Route";
            }
        )
    )
);

Routing parameters look as follows:

"mvc" => array(
    "route" => "/[:controller]/[:action]",
    "method" => "GET|POST",
    "target" => function($request, $response, $args){
        echo "Controller: {$args['controller']}, Action: {$args['action']}";
    }
),

Optional parameters end with "?"

"mvc" => array(
    "route" => "/[:controller]/[:action]?",
    "method" => "GET|POST",
    "target" => function($request, $response, $args){
        echo empty($args["action"]) ? "index"  : $args["action"];
    }
),

You can find more about routing at altorouter.com

Forming urls

$base = hubert()->router->getBasePath();
$url_home = hubert()->router->get("home");
$mvc_url = hubert()->router->get("mvc", ["controller" => "index", "action" => "index"])

The router provides the function getBasePath() that returns the base url of the application. Moreover there is the function get($name, $params = array(), $query = array()) you can use to form urls to defined routes.