class UriBuilder (View source)

An URI Builder

Properties

protected RouterInterface $router
protected Environment $environment
protected BaseUriProvider $baseUriProvider
protected ActionRequest $request
protected array $arguments
protected array $lastArguments

Arguments which have been used for building the last URI

protected string $section
protected bool $createAbsoluteUri
protected bool $addQueryString
protected array $argumentsToBeExcludedFromQueryString
protected string $format

Methods

void
setRequest(ActionRequest $request)

Sets the current request and resets the UriBuilder

getRequest()

Gets the current request

setArguments(array $arguments)

Additional query parameters.

array
getArguments()

No description

setSection(string $section)

If specified, adds a given HTML anchor to the URI (#...)

string
getSection()

No description

setFormat(string $format)

Specifies the format of the target (e.g. "html" or "xml")

string
getFormat()

No description

setCreateAbsoluteUri(bool $createAbsoluteUri)

If set, the URI is prepended with the current base URI. Defaults to false.

bool
getCreateAbsoluteUri()

No description

setAddQueryString(bool $addQueryString)

If set, the current query parameters will be merged with $this->arguments. Defaults to false.

bool
getAddQueryString()

No description

setArgumentsToBeExcludedFromQueryString(array $argumentsToBeExcludedFromQueryString)

A list of arguments to be excluded from the query parameters Only active if addQueryString is set

array
getLastArguments()

Returns the arguments being used for the last URI being built.

reset()

Resets all UriBuilder options to their default value.

string
uriFor(string $actionName, array $controllerArguments = [], string $controllerName = null, string $packageKey = null, string $subPackageKey = null)

Creates an URI used for linking to an Controller action.

array
addNamespaceToArguments(array $arguments, ActionRequest $currentRequest)

Adds the argument namespace of the current request to the specified arguments.

string
build(array $arguments = [])

Builds the URI

array
mergeArgumentsWithRequestArguments(array $arguments)

Merges specified arguments with arguments from request.

string
getRequestNamespacePath(ActionRequest $request)

Get the path of the argument namespaces of all parent requests.

Details

void setRequest(ActionRequest $request)

Sets the current request and resets the UriBuilder

Parameters

ActionRequest $request

Return Value

void

See also

reset()

ActionRequest getRequest()

Gets the current request

Return Value

ActionRequest

UriBuilder setArguments(array $arguments)

Additional query parameters.

If you want to "prefix" arguments, you can pass in multidimensional arrays: array('prefix1' => array('foo' => 'bar')) gets "&prefix1[foo]=bar"

Parameters

array $arguments

Return Value

UriBuilder

the current UriBuilder to allow method chaining

array getArguments()

No description

Return Value

array

UriBuilder setSection(string $section)

If specified, adds a given HTML anchor to the URI (#...)

Parameters

string $section

Return Value

UriBuilder

the current UriBuilder to allow method chaining

string getSection()

No description

Return Value

string

UriBuilder setFormat(string $format)

Specifies the format of the target (e.g. "html" or "xml")

Parameters

string $format

(e.g. "html" or "xml"), will be transformed to lowercase!

Return Value

UriBuilder

the current UriBuilder to allow method chaining

string getFormat()

No description

Return Value

string

UriBuilder setCreateAbsoluteUri(bool $createAbsoluteUri)

If set, the URI is prepended with the current base URI. Defaults to false.

Parameters

bool $createAbsoluteUri

Return Value

UriBuilder

the current UriBuilder to allow method chaining

bool getCreateAbsoluteUri()

No description

Return Value

bool

UriBuilder setAddQueryString(bool $addQueryString)

If set, the current query parameters will be merged with $this->arguments. Defaults to false.

Parameters

bool $addQueryString

Return Value

UriBuilder

the current UriBuilder to allow method chaining

bool getAddQueryString()

No description

Return Value

bool

UriBuilder setArgumentsToBeExcludedFromQueryString(array $argumentsToBeExcludedFromQueryString)

A list of arguments to be excluded from the query parameters Only active if addQueryString is set

Parameters

array $argumentsToBeExcludedFromQueryString

Return Value

UriBuilder

the current UriBuilder to allow method chaining

array getArgumentsToBeExcludedFromQueryString()

No description

Return Value

array

array getLastArguments()

Returns the arguments being used for the last URI being built.

This is only set after build() / uriFor() has been called.

Return Value

array

The last arguments

UriBuilder reset()

Resets all UriBuilder options to their default value.

Note: This won't reset the Request that is attached to this UriBuilder (setRequest())

Return Value

UriBuilder

the current UriBuilder to allow method chaining

string uriFor(string $actionName, array $controllerArguments = [], string $controllerName = null, string $packageKey = null, string $subPackageKey = null)

Creates an URI used for linking to an Controller action.

Parameters

string $actionName

Name of the action to be called

array $controllerArguments

Additional query parameters. Will be merged with $this->arguments.

string $controllerName

Name of the target controller. If not set, current ControllerName is used.

string $packageKey

Name of the target package. If not set, current Package is used.

string $subPackageKey

Name of the target SubPackage. If not set, current SubPackage is used.

Return Value

string

the rendered URI

Exceptions

MissingActionNameException
Exception

See also

build()

protected array addNamespaceToArguments(array $arguments, ActionRequest $currentRequest)

Adds the argument namespace of the current request to the specified arguments.

This happens recursively iterating through the nested requests in case of a subrequest. For example if this is executed inside a widget sub request in a plugin sub request, the result would be: array( 'pluginRequestNamespace' => array( 'widgetRequestNamespace => $arguments ) )

Parameters

array $arguments arguments
ActionRequest $currentRequest

Return Value

array

arguments with namespace

string build(array $arguments = [])

Builds the URI

Parameters

array $arguments

optional URI arguments. Will be merged with $this->arguments with precedence to $arguments

Return Value

string

the (absolute or relative) URI as string

Exceptions

Exception

protected array mergeArgumentsWithRequestArguments(array $arguments)

Merges specified arguments with arguments from request.

If $this->request is no sub request, request arguments will only be merged if $this->addQueryString is set. Otherwise all request arguments except for the ones prefixed with the current request argument namespace will be merged. Additionally special arguments (PackageKey, SubpackageKey, ControllerName & Action) are merged.

The argument provided through the $arguments parameter always overrule the request arguments.

The request hierarchy is structured as follows: root (HTTP) > main (Action) > sub (Action) > sub sub (Action)

Parameters

array $arguments

Return Value

array

protected string getRequestNamespacePath(ActionRequest $request)

Get the path of the argument namespaces of all parent requests.

Example: mainrequest.subrequest.subsubrequest

Parameters

ActionRequest $request

Return Value

string