PHP 8.6.0 Beta 3 is available for testing

Yaf_Route_Regex::__construct

(Yaf >=1.0.0)

Yaf_Route_Regex::__constructYaf_Route_Regex constructor

Опис

public function Yaf_Route_Regex::__construct(
    string $match,
    array $route,
    ?array $map = null,
    ?array $verify = null,
    ?string $reverse = null
)

Constructs a Yaf_Route_Regex route, which routes a request URI matching a regular expression to a module, controller and action.

Параметри

match

A complete regular expression pattern, used to match the request URI. When it does not match, Yaf_Route_Regex::route() returns false and the router moves on to the next route.

route

When the pattern matches the request URI, this array determines the module, controller and action to route to.

Each of the three entries is optional; when one is not given, the default is used. An entry whose value starts with : is resolved against the parameters produced by map.

map

An array mapping capture group indexes to parameter names. Only the captures listed here become request parameters; a numeric capture that is not listed is discarded.

Named capture groups are always added under their own name and do not need to be listed.

verify

Accepted and stored on the route, but not used: the current implementation never consults it while routing.

reverse

A string, used to assemble URL, see Yaf_Route_Regex::assemble().

Зауваження:

This parameter is introduced in 2.3.0

Значення, що повертаються

No value is returned.

Приклади

Приклад #1 Yaf_Route_Regex via Yaf_Router::addRoute()

<?php

/**
 * Add a regex route to the Yaf_Router route stack
 */
Yaf_Dispatcher::getInstance()->getRouter()->addRoute(
    "name",
    new Yaf_Route_Regex(
        "#^/product/([^/]+)/([^/]+)#", // match a URI leading "/product"
        array(
            'controller' => "product", // route to the product controller
        ),
        array(
            1 => "name", // $request->getParam("name") holds the first capture
            2 => "id",   // and $request->getParam("id") the second
        )
    )
);
?>

Приклад #2 Yaf_Route_Regex using a capture as the controller name

<?php

/**
 * Use a capture as the controller name
 */
Yaf_Dispatcher::getInstance()->getRouter()->addRoute(
    "name",
    new Yaf_Route_Regex(
        "#^/product/([^/]+)/([^/]+)#i", // match a URI leading "/product"
        array(
            'controller' => ":name", // the "name" capture (see map) becomes the controller
        ),
        array(
            1 => "name", // $request->getParam("name") holds the first capture
            2 => "id",   // and $request->getParam("id") the second
        )
    )
);
?>

Приклад #3 Yaf_Route_Regex using a named capture group

<?php

/**
 * Use a named capture group as the controller name
 */
Yaf_Dispatcher::getInstance()->getRouter()->addRoute(
    "name",
    new Yaf_Route_Regex(
        "#^/product/(?<name>[^/]+)/([^/]+)#i", // match a URI leading "/product"
        array(
            'controller' => ":name", // the "name" capture becomes the controller
        ),
        array(
            2 => "id", // group 1 is named, only group 2 needs a map entry
        )
    )
);
?>

Приклад #4 Yaf_Route_Regex via Yaf_Router::addConfig()

<?php

/**
 * Add a regex route to the Yaf_Router route stack through a config array
 */
$config = array(
    "name" => array(
        "type"  => "regex",          // a Yaf_Route_Regex route
        "match" => "#(.*)#",         // match any request URI
        "route" => array(
            'controller' => "product", // route to the product controller
            'action'     => "dummy",   // and to the dummy action
        ),
        "map" => array(
            1 => "uri", // $request->getParam("uri") holds the matched URI
        ),
    ),
);

Yaf_Dispatcher::getInstance()->getRouter()->addConfig(
    new Yaf_Config_Simple($config)
);
?>

Прогляньте також

add a note

User Contributed Notes

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