Documentation

UrlGenerator
in package
implements UrlGeneratorInterface, ConfigurableRequirementsInterface

UrlGenerator can generate a URL or a path for any route in the RouteCollection based on the passed parameters.

Tags
author

Fabien Potencier fabien@symfony.com

author

Tobias Schultze http://tobion.de

Interfaces, Classes and Traits

UrlGeneratorInterface
UrlGeneratorInterface is the interface that all URL generator classes must implement.
ConfigurableRequirementsInterface
ConfigurableRequirementsInterface must be implemented by URL generators that can be configured whether an exception should be generated when the parameters do not match the requirements. It is also possible to disable the requirements check for URL generation completely.

Table of Contents

$context  : mixed
$decodedChars  : mixed
This array defines the characters (besides alphanumeric ones) that will not be percent-encoded in the path segment of the generated URL.
$logger  : mixed
$routes  : mixed
$strictRequirements  : bool|null
__construct()  : mixed
generate()  : string
Generates a URL or path for a specific route based on the given parameters.
getContext()  : mixed
{@inheritdoc}
getRelativePath()  : string
Returns the target path as relative reference from the base path.
isStrictRequirements()  : bool|null
Returns whether to throw an exception on incorrect parameters.
setContext()  : mixed
{@inheritdoc}
setStrictRequirements()  : mixed
Enables or disables the exception on incorrect parameters.
doGenerate()  : string|null

Properties

$decodedChars

This array defines the characters (besides alphanumeric ones) that will not be percent-encoded in the path segment of the generated URL.

protected mixed $decodedChars = [ // the slash can be used to designate a hierarchical structure and we want allow using it with this meaning // some webservers don't allow the slash in encoded form in the path for security reasons anyway // see http://stackoverflow.com/questions/4069002/http-400-if-2f-part-of-get-url-in-jboss '%2F' => '/', // the following chars are general delimiters in the URI specification but have only special meaning in the authority component // so they can safely be used in the path in unencoded form '%40' => '@', '%3A' => ':', // these chars are only sub-delimiters that have no predefined meaning and can therefore be used literally // so URI producing applications can use these chars to delimit subcomponents in a path segment without being encoded for better readability '%3B' => ';', '%2C' => ',', '%3D' => '=', '%2B' => '+', '%21' => '!', '%2A' => '*', '%7C' => '|', ]

PHP's rawurlencode() encodes all chars except "a-zA-Z0-9-._~" according to RFC 3986. But we want to allow some chars to be used in their literal form (reasons below). Other chars inside the path must of course be encoded, e.g. "?" and "#" (would be interpreted wrongly as query and fragment identifier), "'" and """ (are used as delimiters in HTML).

$strictRequirements

protected bool|null $strictRequirements = true

Methods

generate()

Generates a URL or path for a specific route based on the given parameters.

public generate(mixed $name[, mixed $parameters = [] ][, mixed $referenceType = self::ABSOLUTE_PATH ]) : string
Parameters
$name : mixed

The name of the route

$parameters : mixed = []

An array of parameters

$referenceType : mixed = self::ABSOLUTE_PATH

The type of reference to be generated (one of the constants)

Return values
string

The generated URL

getContext()

{@inheritdoc}

public getContext() : mixed
Return values
mixed

getRelativePath()

Returns the target path as relative reference from the base path.

public static getRelativePath(string $basePath, string $targetPath) : string

Only the URIs path component (no schema, host etc.) is relevant and must be given, starting with a slash. Both paths must be absolute and not contain relative parts. Relative URLs from one resource to another are useful when generating self-contained downloadable document archives. Furthermore, they can be used to reduce the link size in documents.

Example target paths, given a base path of "/a/b/c/d":

  • "/a/b/c/d" -> ""
  • "/a/b/c/" -> "./"
  • "/a/b/" -> "../"
  • "/a/b/c/other" -> "other"
  • "/a/x/y" -> "../../x/y"
Parameters
$basePath : string

The base path

$targetPath : string

The target path

Return values
string

The relative target path

isStrictRequirements()

Returns whether to throw an exception on incorrect parameters.

public isStrictRequirements() : bool|null
Return values
bool|null

setStrictRequirements()

Enables or disables the exception on incorrect parameters.

public setStrictRequirements(mixed $enabled) : mixed
Parameters
$enabled : mixed
Return values
mixed

doGenerate()

protected doGenerate(mixed $variables, mixed $defaults, mixed $requirements, mixed $tokens, mixed $parameters, mixed $name, mixed $referenceType, mixed $hostTokens[, array<string|int, mixed> $requiredSchemes = [] ]) : string|null
Parameters
$variables : mixed
$defaults : mixed
$requirements : mixed
$tokens : mixed
$parameters : mixed
$name : mixed
$referenceType : mixed
$hostTokens : mixed
$requiredSchemes : array<string|int, mixed> = []
Tags
throws
MissingMandatoryParametersException

When some parameters are missing that are mandatory for the route

throws
InvalidParameterException

When a parameter value for a placeholder is not correct because it does not match the requirement

Return values
string|null

Search results