(Yaf >=2.3.0)
Yaf_Route_Interface::assemble — Assemble a request
this method returns a url according to the argument info, and append query strings to the url according to the argument query.
a route should implement this method according to its own route rules, and do a reverse progress.
info
query
Example #1 Yaf_Route_Interface::assemble() example
<?php
class RewriteRoute implements Yaf_Route_Interface {
private $_match;
private $_route;
public function __construct(string $match, array $route) {
$this->_match = $match;
$this->_route = $route;
}
public function route(Yaf_Request_Abstract $request): bool {
if (!preg_match($this->_match, $request->getRequestUri(), $matches)) {
return false;
}
foreach ($this->_route as $key => $value) {
if (is_string($value) && ':' === $value[0]) {
$value = $matches[substr($value, 1)];
}
$request->setParam($key, $value);
}
$request->setRouted();
return true;
}
/* reverse the route rules back into a URL */
public function assemble(array $info, ?array $query = null): string {
$url = "/product";
if (isset($info[':name'])) {
$url .= "/" . $info[':name'];
}
if (!empty($query)) {
$url .= "?" . http_build_query($query);
}
return $url;
}
}
$router = new Yaf_Router();
$router->addRoute("custom",
new RewriteRoute("#^/product#", array("controller" => "product"))
);
var_dump($router->getRoute("custom")->assemble(
array(':name' => 'book'),
array('page' => 2)
));
?>The above example will output something similar to:
string(20) "/product/book?page=2"