W3cubDocs

/Symfony 4.1

UrlGenerator

class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInterface

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

Properties

protected $routes
protected $context
protected bool|null $strictRequirements
protected $logger
protected $decodedChars This array defines the characters (besides alphanumeric ones) that will not be percent-encoded in the path segment of the generated URL.

Methods

__construct(RouteCollection $routes, RequestContext $context, LoggerInterface $logger = null, string $defaultLocale = null)
setContext(RequestContext $context)

Sets the request context.

RequestContext getContext()

Gets the request context.

setStrictRequirements(bool|null $enabled)

Enables or disables the exception on incorrect parameters.

bool|null isStrictRequirements()

Returns whether to throw an exception on incorrect parameters.

string generate(string $name, mixed $parameters = array(), int $referenceType = self::ABSOLUTE_PATH)

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

doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, array $requiredSchemes = array())
static string getRelativePath(string $basePath, string $targetPath)

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

Details

__construct(RouteCollection $routes, RequestContext $context, LoggerInterface $logger = null, string $defaultLocale = null)

Parameters

RouteCollection $routes
RequestContext $context
LoggerInterface $logger
string $defaultLocale

setContext(RequestContext $context)

Sets the request context.

Parameters

RequestContext $context

RequestContext getContext()

Gets the request context.

Return Value

RequestContext The context

setStrictRequirements(bool|null $enabled)

Enables or disables the exception on incorrect parameters.

Passing null will deactivate the requirements check completely.

Parameters

bool|null $enabled

bool|null isStrictRequirements()

Returns whether to throw an exception on incorrect parameters.

Null means the requirements check is deactivated completely.

Return Value

bool|null

string generate(string $name, mixed $parameters = array(), int $referenceType = self::ABSOLUTE_PATH)

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

Parameters that reference placeholders in the route pattern will substitute them in the path or host. Extra params are added as query string to the URL.

When the passed reference type cannot be generated for the route because it requires a different host or scheme than the current one, the method will return a more comprehensive reference that includes the required params. For example, when you call this method with $referenceType = ABSOLUTE_PATH but the route requires the https scheme whereas the current scheme is http, it will instead return an ABSOLUTE_URL with the https scheme and the current host. This makes sure the generated URL matches the route in any case.

If there is no route with the given name, the generator must throw the RouteNotFoundException.

The special parameter _fragment will be used as the document fragment suffixed to the final URL.

Parameters

string $name The name of the route
mixed $parameters An array of parameters
int $referenceType The type of reference to be generated (one of the constants)

Return Value

string The generated URL

Exceptions

RouteNotFoundException If the named route doesn't exist
MissingMandatoryParametersException When some parameters are missing that are mandatory for the route
InvalidParameterException When a parameter value for a placeholder is not correct because it does not match the requirement

protected doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, array $requiredSchemes = array())

Parameters

$variables
$defaults
$requirements
$tokens
$parameters
$name
$referenceType
$hostTokens
array $requiredSchemes

Exceptions

MissingMandatoryParametersException When some parameters are missing that are mandatory for the route
InvalidParameterException When a parameter value for a placeholder is not correct because it does not match the requirement

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

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

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

string $basePath The base path
string $targetPath The target path

Return Value

string The relative target path

© 2004–2017 Fabien Potencier
Licensed under the MIT License.
https://api.symfony.com/4.1/Symfony/Component/Routing/Generator/UrlGenerator.html