PHP 8.4.26 Released!

Yaf_Route_Interface::assemble

(Yaf >=2.3.0)

Yaf_Route_Interface::assemble — Ensamblar una petición

Descripción

abstract public function Yaf_Route_Interface::assemble(array $info, array $query = ?): string

Este método devuelve un URL según el argumento info info, y pospone los string de consultas al URL según el argumento query.

Una ruta debería implementar este método según sus propias reglas de ruta, y realizar un progreso inverso.

Parámetros

info

query

Valores devueltos

Ejemplos

Ejemplo #1 Ejemplo de Yaf_Route_Interface::assemble()

<?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;
    }

    /* reconstruye una URL a partir de las reglas de la ruta */
    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)
));
?>

Resultado del ejemplo anterior es similar a:

string(20) "/product/book?page=2"

Véase también

+add a note

User Contributed Notes

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