url_helper.php
This file is part of CodeIgniter 4 framework.
(c) CodeIgniter Foundation admin@codeigniter.com
For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
Table of Contents
Functions
- site_url() : string
- Returns a site URL as defined by the App config.
- base_url() : string
- Returns the base URL as defined by the App config.
- current_url() : string|URI
- Returns the current full URL based on the Config\App settings and IncomingRequest.
- previous_url() : string|URI
- Returns the previous URL the current visitor was on. For security reasons we first check in a saved session variable, if it exists, and use that.
- uri_string() : string
- URL String
- index_page() : string
- Index page
- anchor() : string
- Anchor Link
- anchor_popup() : string
- Anchor Link - Pop-up version
- mailto() : string
- Mailto Link
- safe_mailto() : string
- Encoded Mailto Link
- auto_link() : string
- Auto-linker
- prep_url() : string
- Prep URL - Simply adds the http:// or https:// part if no scheme is included.
- url_title() : string
- Create URL Title
- mb_url_title() : string
- Create URL Title that takes into account accented characters
- url_to() : string
- Get the full, absolute URL to a route name or controller method (with additional arguments)
- url_is() : bool
- Determines if current url path contains the given path. It may contain a wildcard (*) which will allow any valid character.
Functions
site_url()
Returns a site URL as defined by the App config.
site_url([array<string|int, mixed>|string $relativePath = '' ][, string|null $scheme = null ][, App|null $config = null ]) : string
Parameters
- $relativePath : array<string|int, mixed>|string = ''
-
URI string or array of URI segments.
- $scheme : string|null = null
-
URI scheme. E.g., http, ftp. If empty string '' is set, a protocol-relative link is returned.
- $config : App|null = null
-
Alternate configuration to use.
Return values
stringbase_url()
Returns the base URL as defined by the App config.
base_url([array<string|int, mixed>|string $relativePath = '' ][, string|null $scheme = null ]) : string
Base URLs are trimmed site URLs without the index page.
Parameters
- $relativePath : array<string|int, mixed>|string = ''
-
URI string or array of URI segments.
- $scheme : string|null = null
-
URI scheme. E.g., http, ftp. If empty string '' is set, a protocol-relative link is returned.
Return values
stringcurrent_url()
Returns the current full URL based on the Config\App settings and IncomingRequest.
current_url([bool $returnObject = false ][, IncomingRequest|null $request = null ]) : string|URI
Parameters
- $returnObject : bool = false
-
True to return an object instead of a string
- $request : IncomingRequest|null = null
-
A request to use when retrieving the path
Return values
string|URI —When returning string, the query and fragment parts are removed. When returning URI, the query and fragment parts are preserved.
previous_url()
Returns the previous URL the current visitor was on. For security reasons we first check in a saved session variable, if it exists, and use that.
previous_url([bool $returnObject = false ]) : string|URI
If that's not available, however, we'll use a sanitized url from $_SERVER['HTTP_REFERER'] which can be set by the user so is untrusted and not set by certain browsers/servers.
Parameters
- $returnObject : bool = false
Return values
string|URIuri_string()
URL String
uri_string() : string
Returns the path part (relative to baseURL) of the current URL
Return values
stringindex_page()
Index page
index_page([App|null $altConfig = null ]) : string
Returns the "index_page" from your config file
Parameters
- $altConfig : App|null = null
-
Alternate configuration to use
Return values
stringanchor()
Anchor Link
anchor([array<string|int, mixed>|string $uri = '' ][, string $title = '' ][, array<string|int, mixed>|object|string $attributes = '' ][, App|null $altConfig = null ]) : string
Creates an anchor based on the local URL.
Parameters
- $uri : array<string|int, mixed>|string = ''
-
URI string or array of URI segments
- $title : string = ''
-
The link title
- $attributes : array<string|int, mixed>|object|string = ''
-
Any attributes
- $altConfig : App|null = null
-
Alternate configuration to use
Return values
stringanchor_popup()
Anchor Link - Pop-up version
anchor_popup([string $uri = '' ][, string $title = '' ][, array<string|int, mixed>|false|object|string $attributes = false ][, App|null $altConfig = null ]) : string
Creates an anchor based on the local URL. The link opens a new window based on the attributes specified.
Parameters
- $uri : string = ''
-
the URL
- $title : string = ''
-
the link title
- $attributes : array<string|int, mixed>|false|object|string = false
-
any attributes
- $altConfig : App|null = null
-
Alternate configuration to use
Return values
stringmailto()
Mailto Link
mailto(string $email[, string $title = '' ][, array<string|int, mixed>|object|string $attributes = '' ]) : string
Parameters
- $email : string
-
the email address
- $title : string = ''
-
the link title
- $attributes : array<string|int, mixed>|object|string = ''
-
any attributes
Return values
stringsafe_mailto()
Encoded Mailto Link
safe_mailto(string $email[, string $title = '' ][, array<string|int, mixed>|object|string $attributes = '' ]) : string
Create a spam-protected mailto link written in Javascript
Parameters
- $email : string
-
the email address
- $title : string = ''
-
the link title
- $attributes : array<string|int, mixed>|object|string = ''
-
any attributes
Return values
stringauto_link()
Auto-linker
auto_link(string $str[, string $type = 'both' ][, bool $popup = false ]) : string
Automatically links URL and Email addresses. Note: There's a bit of extra code here to deal with URLs or emails that end in a period. We'll strip these off and add them after the link.
Parameters
- $str : string
-
the string
- $type : string = 'both'
-
the type: email, url, or both
- $popup : bool = false
-
whether to create pop-up links
Return values
stringprep_url()
Prep URL - Simply adds the http:// or https:// part if no scheme is included.
prep_url([string $str = '' ][, bool $secure = false ]) : string
Formerly used URI, but that does not play nicely with URIs missing the scheme.
Parameters
- $str : string = ''
-
the URL
- $secure : bool = false
-
set true if you want to force https://
Return values
stringurl_title()
Create URL Title
url_title(string $str[, string $separator = '-' ][, bool $lowercase = false ]) : string
Takes a "title" string as input and creates a human-friendly URL string with a "separator" string as the word separator.
Parameters
- $str : string
-
Input string
- $separator : string = '-'
-
Word separator (usually '-' or '_')
- $lowercase : bool = false
-
Whether to transform the output string to lowercase
Return values
stringmb_url_title()
Create URL Title that takes into account accented characters
mb_url_title(string $str[, string $separator = '-' ][, bool $lowercase = false ]) : string
Takes a "title" string as input and creates a human-friendly URL string with a "separator" string as the word separator.
Parameters
- $str : string
-
Input string
- $separator : string = '-'
-
Word separator (usually '-' or '_')
- $lowercase : bool = false
-
Whether to transform the output string to lowercase
Return values
stringurl_to()
Get the full, absolute URL to a route name or controller method (with additional arguments)
url_to(string $controller, int|string ...$args) : string
NOTE: This requires the controller/method to have a route defined in the routes Config file.
Parameters
- $controller : string
-
Route name or Controller::method
- $args : int|string
-
One or more parameters to be passed to the route. The last parameter allows you to set the locale.
Tags
Return values
stringurl_is()
Determines if current url path contains the given path. It may contain a wildcard (*) which will allow any valid character.
url_is(string $path) : bool
Example: if (url_is('admin*')) ...
Parameters
- $path : string