URL

The URL utility provides a collection of helper functions to assist developers in building routes and performing redirects.

Route

Returns the current request's route. From the url https://domain.com/route/to/page?q=search+query, returns /route/to/page

url::route()

Parse QueryString

Returns the query string of the current request. From the url https://domain.com/route/to/page?q=search+query returns ?q=search+query.

url::queryString()

File Type

Returns the file extension from a URL or null if not extension is found.

url::getFileType()

URI

Returns the decoded URI of the current request.

Route Parts

Explodes the a request's route by '/' and returns the resulting parts.

url::parts('http://domain.com/articles/title-of-article/1')

// Returns
array (
    'articles,
    'title-of-article',
    '1'
)

Host

Returns the current request's host.

url::host()

Protocol

Returns the current request's protocol. Expected return values are http:// or https://.

url::protocol()

HTTP Method

Returns the HTTP method used in the current request. Valid return values include get, post, put, patch, delete.

url::method()

Make URL From Route

A route should only be hard-coded in once place: the module config. From there, it should never be directly referenced again. The make() method is used to build URLs by referencing the name of the route, instead of the route's actual path. This allows developers to change routes without breaking links throughout the application.

Consider the following route definitions:

array(
    new saRoute(array(
        'id'            => 'members_users_list',
        'name'          => 'Our Users',
        'route'         => '/users/list',
        'controller'    => 'UsersController@index',
        'method'        => 'GET'
    )),
    new saRoute(array(
        'id'            => 'members_account',
        'name'          => 'Account',
        'route'         => '^/users/[0-9]{1,}$',
        'controller'    => 'UsersController@account',
        'method'        => 'GET'
    )),
    new saRoute(array(
            'id'            => 'members_user_invoice',
            'name'          => 'Invoice',
            'route'         => '^/users/[0-9]{1,}/invoice/[0-9]{1,}$',
            'controller'    => 'UsersController@account',
            'method'        => 'GET'
        ))
);

The members_users_list route doesn't have any dynamic parameters. The members_account route has 1 dynamic parameter (a user Id).

// Returns "/users/list"
url::make('members_users_list');

// Returns "/users/12"
$userId = 12;
url::make('members_account', $userId);

// Returns "/users/12/invoice/1111111"
$userId = 12;
$invoiceId = 1111111;
url::make('members_user_invoice', $userId, $invoiceId);

The url::make() method's first parameter is the name of the route. The number of subsequent parameters will always match the number of dynamic parameters in the route.

e.g To build a route with 4 dynamic parameters, the make() method will require 4 additional parameters after the route name.

Redirect

To force a page redirect in any part of the application, use the redirect() method.

This method is meant for redirecting to third-party URLs. To redirect to an application route, use redirectId().

url::redirect('http://domain.com', 'q=search+keywords', 302)

Parameter

Type

Required

Description

$url

string

yes

The URL to redirect to.

$queryString

string

no

The URL's query string.

$redirectCode

int

no

Default: 302. The redirect type (301 or 302).

Redirect to Route

Redirects to a named route in the application.

// Redirects to member account page. Route requires 1 parameter: user ID.
// Resulting URL: "/users/1".
url::redirectId('members_account', 1)

isCurrentRoute

Compares the route of the current request's with a given route.

// Compares "/users/1" with current route
url::isCurrentRoute('members_account', 1)

Find route by ID

See the routing documentation.

Last updated