URL Helper

The URL Helper file contains functions that assist in working with URLs.

Loading this Helper

This helper is automatically loaded by the framework on every request.

Available Functions

The following functions are available:

site_url([$uri = ''[, $protocol = null[, $altConfig = null]]])
參數:
  • $uri (mixed) – URI string or array of URI segments
  • $protocol (string) – Protocol, e.g., 『http』 or 『https』
  • $altConfig (\Config\App) – Alternate configuration to use
傳回:

Site URL

傳回型態:

string

Returns your site URL, as specified in your config file. The index.php file (or whatever you have set as your site indexPage in your config file) will be added to the URL, as will any URI segments you pass to the function.

You are encouraged to use this function any time you need to generate a local URL so that your pages become more portable in the event your URL changes.

Segments can be optionally passed to the function as a string or an array. Here is a string example:

echo site_url('news/local/123');

The above example would return something like: http://example.com/index.php/news/local/123

Here is an example of segments passed as an array:

$segments = ['news', 'local', '123'];
echo site_url($segments);

You may find the alternate configuration useful if generating URLs for a different site than yours, which contains different configuration preferences. We use this for unit testing the framework itself.

base_url([$uri = ''[, $protocol = null]])
參數:
  • $uri (mixed) – URI string or array of URI segments
  • $protocol (string) – Protocol, e.g., 『http』 or 『https』
傳回:

Base URL

傳回型態:

string

Returns your site base URL, as specified in your config file. Example:

echo base_url();

This function returns the same thing as site_url(), without the indexPage being appended.

Also like site_url(), you can supply segments as a string or an array. Here is a string example:

echo base_url('blog/post/123');

The above example would return something like: http://example.com/blog/post/123

This is useful because unlike site_url(), you can supply a string to a file, such as an image or stylesheet. For example:

echo base_url('images/icons/edit.png');

This would give you something like: http://example.com/images/icons/edit.png

current_url([$returnObject = false[, $request = null]])
參數:
  • $returnObject (boolean) – True if you would like a URI instance returned, instead of a string.
  • $request (IncomingRequest|null) – An alternate request to use for path detection; useful for testing.
傳回:

The current URL

傳回型態:

string|\CodeIgniter\HTTP\URI

Returns the full URL (including segments) of the page being currently viewed.

備註

Calling this function is the same as doing this:

site_url(uri_string());

重要

Prior to 4.1.2 this function had a bug causing it to ignore the configuration on App::$indexPage.

previous_url([$returnObject = false])
參數:
  • $returnObject (boolean) – True if you would like a URI instance returned instead of a string.
傳回:

The URL the user was previously on

傳回型態:

string|mixed|\CodeIgniter\HTTP\URI

Returns the full URL (including segments) of the page the user was previously on.

Due to security issues of blindly trusting the HTTP_REFERER system variable, CodeIgniter will store previously visited pages in the session if it’s available. This ensures that we always use a known and trusted source. If the session hasn’t been loaded, or is otherwise unavailable, then a sanitized version of HTTP_REFERER will be used.

uri_string([$relative = false])
參數:
  • $relative (boolean) – True if you would like the string relative to baseURL
傳回:

A URI string

傳回型態:

string

Returns the path part of the current URL. For example, if your URL was this:

http://some-site.com/blog/comments/123

The function would return:

/blog/comments/123

Or with the optional relative parameter:

app.baseURL = http://some-site.com/subfolder/

uri_string(); // "/subfolder/blog/comments/123"
uri_string(true); // "blog/comments/123"
index_page([$altConfig = null])
參數:
  • $altConfig (\Config\App) – Alternate configuration to use
傳回:

『index_page』 value

傳回型態:

string

Returns your site indexPage, as specified in your config file. Example:

echo index_page();

As with site_url(), you may specify an alternate configuration. You may find the alternate configuration useful if generating URLs for a different site than yours, which contains different configuration preferences. We use this for unit testing the framework itself.

anchor([$uri = ''[, $title = ''[, $attributes = ''[, $altConfig = null]]]])
參數:
  • $uri (mixed) – URI string or array of URI segments
  • $title (string) – Anchor title
  • $attributes (mixed) – HTML attributes
  • $altConfig (\Config\App) – Alternate configuration to use
傳回:

HTML hyperlink (anchor tag)

傳回型態:

string

Creates a standard HTML anchor link based on your local site URL.

The first parameter can contain any segments you wish appended to the URL. As with the site_url() function above, segments can be a string or an array.

備註

If you are building links that are internal to your application do not include the base URL (http://…). This will be added automatically from the information specified in your config file. Include only the URI segments you wish appended to the URL.

The second segment is the text you would like the link to say. If you leave it blank, the URL will be used.

The third parameter can contain a list of attributes you would like added to the link. The attributes can be a simple string or an associative array.

Here are some examples:

echo anchor('news/local/123', 'My News', 'title="News title"');
// Prints: <a href="http://example.com/index.php/news/local/123" title="News title">My News</a>

echo anchor('news/local/123', 'My News', ['title' => 'The best news!']);
// Prints: <a href="http://example.com/index.php/news/local/123" title="The best news!">My News</a>

echo anchor('', 'Click here');
// Prints: <a href="http://example.com/index.php">Click here</a>

As above, you may specify an alternate configuration. You may find the alternate configuration useful if generating links for a different site than yours, which contains different configuration preferences. We use this for unit testing the framework itself.

備註

Attributes passed into the anchor function are automatically escaped to protected against XSS attacks.

anchor_popup([$uri = ''[, $title = ''[, $attributes = false[, $altConfig = null]]]])
參數:
  • $uri (string) – URI string
  • $title (string) – Anchor title
  • $attributes (mixed) – HTML attributes
  • $altConfig (\Config\App) – Alternate configuration to use
傳回:

Pop-up hyperlink

傳回型態:

string

Nearly identical to the anchor() function except that it opens the URL in a new window. You can specify JavaScript window attributes in the third parameter to control how the window is opened. If the third parameter is not set it will simply open a new window with your own browser settings.

Here is an example with attributes:

$atts = [
    'width'       => 800,
    'height'      => 600,
    'scrollbars'  => 'yes',
    'status'      => 'yes',
    'resizable'   => 'yes',
    'screenx'     => 0,
    'screeny'     => 0,
    'window_name' => '_blank',
];

echo anchor_popup('news/local/123', 'Click Me!', $atts);

As above, you may specify an alternate configuration. You may find the alternate configuration useful if generating links for a different site than yours, which contains different configuration preferences. We use this for unit testing the framework itself.

備註

The above attributes are the function defaults so you only need to set the ones that are different from what you need. If you want the function to use all of its defaults simply pass an empty array in the third parameter:

echo anchor_popup('news/local/123', 'Click Me!', []);

備註

The window_name is not really an attribute, but an argument to the JavaScript window.open() method, which accepts either a window name or a window target.

備註

Any other attribute than the listed above will be parsed as an HTML attribute to the anchor tag.

備註

Attributes passed into the anchor_popup function are automatically escaped to protected against XSS attacks.

mailto($email[, $title = ''[, $attributes = '']])
參數:
  • $email (string) – E-mail address
  • $title (string) – Anchor title
  • $attributes (mixed) – HTML attributes
傳回:

A 「mail to」 hyperlink

傳回型態:

string

Creates a standard HTML e-mail link. Usage example:

echo mailto('me@my-site.com', 'Click Here to Contact Me');

As with the anchor() tab above, you can set attributes using the third parameter:

$attributes = ['title' => 'Mail me'];
echo mailto('me@my-site.com', 'Contact Me', $attributes);

備註

Attributes passed into the mailto function are automatically escaped to protected against XSS attacks.

safe_mailto($email[, $title = ''[, $attributes = '']])
參數:
  • $email (string) – E-mail address
  • $title (string) – Anchor title
  • $attributes (mixed) – HTML attributes
傳回:

A spam-safe 「mail to」 hyperlink

傳回型態:

string

Identical to the mailto() function except it writes an obfuscated version of the mailto tag using ordinal numbers written with JavaScript to help prevent the e-mail address from being harvested by spam bots.

參數:
  • $str (string) – Input string
  • $type (string) – Link type (『email』, 『url』 or 『both』)
  • $popup (bool) – Whether to create popup links
傳回:

Linkified string

傳回型態:

string

Automatically turns URLs and e-mail addresses contained in a string into links. Example:

$string = auto_link($string);

The second parameter determines whether URLs and e-mails are converted or just one or the other. The default behavior is both if the parameter is not specified. E-mail links are encoded as safe_mailto() as shown above.

Converts only URLs:

$string = auto_link($string, 'url');

Converts only e-mail addresses:

$string = auto_link($string, 'email');

The third parameter determines whether links are shown in a new window. The value can be true or false (boolean):

$string = auto_link($string, 'both', true);

備註

The only URLs recognized are those that start with 「www.」 or with 「://」.

url_title($str[, $separator = '-'[, $lowercase = false]])
參數:
  • $str (string) – Input string
  • $separator (string) – Word separator (usually 『-『 or 『_』)
  • $lowercase (bool) – Whether to transform the output string to lowercase
傳回:

URL-formatted string

傳回型態:

string

Takes a string as input and creates a human-friendly URL string. This is useful if, for example, you have a blog in which you’d like to use the title of your entries in the URL. Example:

$title     = "What's wrong with CSS?";
$url_title = url_title($title);
// Produces: Whats-wrong-with-CSS

The second parameter determines the word delimiter. By default dashes are used. Preferred options are: - (dash) or _ (underscore).

Example:

$title     = "What's wrong with CSS?";
$url_title = url_title($title, '_');
// Produces: Whats_wrong_with_CSS

The third parameter determines whether or not lowercase characters are forced. By default they are not. Options are boolean true/false.

Example:

$title     = "What's wrong with CSS?";
$url_title = url_title($title, '-', true);
// Produces: whats-wrong-with-css
mb_url_title($str[, $separator = '-'[, $lowercase = false]])
參數:
  • $str (string) – Input string
  • $separator (string) – Word separator (usually 『-『 or 『_』)
  • $lowercase (bool) – Whether to transform the output string to lowercase
傳回:

URL-formatted string

傳回型態:

string

This function works the same as url_title() but it converts all accented characters automatically.

prep_url([$str = ''[, $secure = false]])
參數:
  • $str (string) – URL string
  • $secure (boolean) – true for https://
傳回:

Protocol-prefixed URL string

傳回型態:

string

This function will add http:// or https:// in the event that a protocol prefix is missing from a URL.

Pass the URL string to the function like this:

$url = prep_url('example.com');
url_to($controller[, ...$args])
參數:
  • $controller (string) – The controller class and method
  • ...$args (mixed) – Additional arguments to be injected into the route
傳回:

Absolute URL

傳回型態:

string

Builds an absolute URL to a controller method in your app. Example:

echo url_to('Home::index');

You can also add arguments to the route. Here is an example:

echo url_to('Page::index', 'home');

The above example would return something like: http://example.com/page/home

This is useful because you can still change your routes after putting links into your views.

url_is($path)
參數:
  • $path (string) – The path to check the current URI path against.
傳回型態:

boolean

Compares the current URL’s path against the given path to see if they match. Example:

if (url_is('admin')) { ... }

This would match http://example.com/admin. You can use the * wildcard to match any other applicable characters in the URL:

if (url_is('admin*')) { ... }

This would match any of the following:

  • /admin
  • /admin/
  • /admin/users
  • /admin/users/schools/classmates/…