Yaf_Router::route

(Yaf >=1.0.0)

Yaf_Router::routeRoute a request

Description

public function Yaf_Router::route(Yaf_Request_Abstract $request): ?bool

Route the request with the registered routes.

Routes are tried in reverse order of registration. The first route that claims the request wins; the request is then marked as routed (see Yaf_Request_Abstract::setRouted()) and the name of the winning route is recorded, which can be retrieved via Yaf_Router::getCurrentRoute().

The default route, registered as _default, is Yaf_Route_Static unless a custom default route is configured.

Parameters

request

The Yaf_Request_Abstract instance to route.

Return Values

Returns true if the request has been routed, or false if no route matched (e.g. a malformed URI), which makes the dispatcher stop.

Examples

Example #1 Yaf_Router::route() example

<?php
class Bootstrap extends Yaf_Bootstrap_Abstract
{
    public function _initRoute(Yaf_Dispatcher $dispatcher)
    {
        $router = $dispatcher->getRouter();

        $router->addRoute("rewrite", new Yaf_Route_Rewrite(
            "/product/:id",
            ["controller" => "product", "action" => "detail"]
        ));

        /* route a hand-built request, e.g. in a unit test */
        $request = new Yaf_Request_Simple("CLI", null, "index.php",
            "/product/123", null);

        if ($router->route($request)) {
            var_dump($router->getCurrentRoute());
            var_dump($request->getControllerName());
            var_dump($request->getActionName());
            var_dump($request->getParam("id"));
        } else {
            /* no route matched */
        }
    }
}
?>

The above example will output something similar to:

string(7) "rewrite"
string(7) "product"
string(6) "detail"
string(3) "123"

See Also

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top