Skip to content

UrlGenerator ​

Solo\Router\UrlGenerator generates the paths of the named routes of a Router. See URL Generation for examples.

php
use Solo\Router\UrlGenerator;

$urls = new UrlGenerator($router);

It works with any router, including one restored by RouteCache or Router::fromArray().

generate() ​

php
public function generate(
    string $name,
    array $params = [],
    array $query = []
): string
ArgumentTypeMeaning
$namestringRoute name
$paramsarray<string, string|int|null>Values by parameter name; null counts as no value
$queryarrayQuery values, encoded by http_build_query() with PHP_QUERY_RFC3986

Returns the path of the route with its parameters filled in, followed by ? and the query string when $query produces one:

  • literal text of the path stays as it is;
  • {name} and {name:regex} take the value encoded with rawurlencode(), keeping / where the pattern accepts it; the encoded value must match the pattern;
  • an optional segment [...] is generated only if a parameter in it has a value, and its other parameters are then required;
  • a path left empty, e.g. by [/] without a value for an optional prefix, becomes /.

Throws InvalidArgumentException if no route has the name, if a parameter is missing, unknown, or does not match its pattern, or if the path contains a raw regex group (...).

Released under the MIT License.