This is documentation for Kohana v2.3.x.

Table of Contents
StatusDraft
TodoProof read

URL Helper

Provides methods for working with URL(s).

Methods

current()

url::current() returns the current URI string. This method accepts one optional parameter. If you set it to TRUE the query string will be included in the return value.

// site_domain = 'localhost/kohana/'
// site_protocol = 'http'
// index_page = 'index.php'
// url_suffix = '.php'
 
// Current URL: http://localhost/kohana/index.php/welcome/home.php?query=string
 
echo url::current();

Returns

welcome/home

While

echo url::current(TRUE);

Returns

welcome/home?query=string

base()

url::base() returns the base URL defined by the site_protocol and site_domain in config.php. If your site_domain doesn't have a hostname in it, you will get a relative URL.

// site_domain = 'localhost/kohana/'
// site_protocol = 'http'
 
echo url::base();

Generates

http://localhost/kohana/

url::base() accepts two optional parameters. Set the first parameter to TRUE if you want to append the index_page defined in config.php to the base URL. Via the second parameter you can overwrite the default site_protocol from config.php.

// site_domain = 'localhost/kohana/'
// site_protocol = 'http'
// index_page = 'index.php'
 
echo url::base(TRUE, 'https');

Generates

https://localhost/kohana/index.php

site()

url::site() returns a URL based on the site_protocol, site_domain, index_page, url_suffix defined in config.php. If your site_domain doesn't have a hostname in it, you will get a relative URL.

// site_domain = 'localhost/kohana/'
// site_protocol = 'http'
// index_page = 'index.php'
// url_suffix = '.php'
 
echo url::site();

Generates

http://localhost/kohana/index.php/.php

url::site() accepts two optional parameters. You can pass URL segments via the first one. The second one allows you to overwrite the default site_protocol from config.php.

// site_domain = 'localhost/kohana/'
// site_protocol = 'http'
// index_page = ''
// url_suffix = '.php'
 
echo url::site('admin/login', 'https');

Generates

https://localhost/kohana/admin/login.php

file()

url::file() returns the URL to a file. Absolute filenames and relative filenames are allowed.

echo url::file('download.zip');

Produces

http://domain.tld/download.zip

title()

url::title() returns a properly formatted title, for use in a URI. The first parameter, the input title string, is mandatory. The optional second parameter is used to set the separator character. By default this is a dash. You can only change this to an underscore.

$input_title = ' __Ecléçtic__ title\'s  entered by cràzed users- ?>  ';
 
echo url::title($input_title, '_');

Generates:

eclectic_titles_entered_by_crazed_users

What happens to the input title? All non-alphanumeric characters, except for dashes or underscores (depending on the second parameter), will be deleted. However, non-ascii characters will first be transliterated (for example: à becomes a) in order to keep the URL title as readable as possible. Finally, the URL title is converted to lowercase.

redirect()

url::redirect() generates an HTTP Server Header (302) and runs the system.redirect event, which will redirect the browser to a specified URL, by default site_domain defined in config.php.

url::redirect() will always call the php exit function to prevent further script execution.

url::redirect('http://www.whitehouse.gov');

Will redirect the browser to the White House website.

The optional second parameter can be used to set the redirect method. The default is 302.

// site_domain = 'localhost/kohana/'
// site_protocol = 'http'
 
url::redirect('aboutus', 301);

Will redirect with a 301 header to http://localhost/kohana/aboutus.

If you wish to send a Multiple Choice (300) redirect, provide an array of URIs to the redirect method:

url::redirect(array('aboutus','http://www.kohana.php/'), 300);

The first URI in the array is considered the preferred URI and will be placed in the Location header. All of the URIs will then be output in a HTML unordered list. Generally the browser will follow the location header and this list will never be seen. However, there is no standard defined behavior for what a user agent should do upon receiving a 300 and the list could be used to present the user with the choices you have given.