Helpers

In This Article

Url

The URL view helper is used to create a string representation of the routes that
you define within your application. The syntax for the view helper is
$this->url($name, $params, $options, $reuseMatchedParameters), using the
following definitions for the helper arguments:

$name: The name of the route you want to output.

$params: An array of parameters that is defined within the respective route
configuration.

$options: An array of options that will be used to create the URL.

$reuseMatchedParams: A flag indicating if the currently matched route
parameters should be used when generating the new URL.

Let's take a look at how this view helper is used in real-world applications.

Basic Usage

The following example shows a simple configuration for a news module. The route
is called news and it has two optional parameters called action and
id.

Query String Arguments

Most SEO experts agree that pagination parameters should not be part of the URL
path; for example, the following URL would be considered a bad practice:
/news/archive/page/13. Pagination is more correctly accomplished using a query
string arguments, such as /news/archive?page=13. To achieve this, you'll need
to make use of the $options argument from the view helper.

Fragments

Another possible entry within the $options array is the assignment of URL
fragments (typically used to link to in-page anchors), denoted with using the
fragment key. Let's assume we want to enter a link for users to directly jump
to the comment section of a details page:

Reusing Matched Parameters

When you're on a route that has many parameters, often times it makes sense to
reuse currently matched parameters instead of assigning them explicitly. In this
case, the argument $reuseMatchedParams will come in handy.

As an example, we will imagine being on a detail page for our news route. We
want to display links to the edit and delete actions without having to
assign the ID again:

Shorthand

Due to the fact that reusing parameters is a use case that can happen when no
route options are set, the third argument for the URL view helper will be
checked against its type; when a boolean is passed, the helper uses it to set
the value of the $reuseMatchedParams flag: