########## URL Helper ########## The URL Helper file contains functions that assist in working with URLs. .. contents:: :local: .. raw:: html
Loading this Helper =================== This helper is loaded using the following code:: $this->load->helper('url'); Available Functions =================== The following functions are available: .. php:function:: site_url([$uri = ''[, $protocol = NULL]]) :param string $uri: URI string :param string $protocol: Protocol, e.g. 'http' or 'https' :returns: Site URL :rtype: string Returns your site URL, as specified in your config file. The index.php file (or whatever you have set as your site **index_page** in your config file) will be added to the URL, as will any URI segments you pass to the function, plus the **url_suffix** as set in your config file. 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 = array('news', 'local', '123'); echo site_url($segments); This function is an alias for ``CI_Config::site_url()``. For more info, please see the :doc:`Config Library <../libraries/config>` documentation. .. php:function:: base_url($uri = '', $protocol = NULL) :param string $uri: URI string :param string $protocol: Protocol, e.g. 'http' or 'https' :returns: Base URL :rtype: string Returns your site base URL, as specified in your config file. Example:: echo base_url(); This function returns the same thing as :php:func:`site_url()`, without the *index_page* or *url_suffix* being appended. Also like :php:func:`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 :php:func:`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* This function is an alias for ``CI_Config::base_url()``. For more info, please see the :doc:`Config Library <../libraries/config>` documentation. .. php:function:: current_url() :returns: The current URL :rtype: string Returns the full URL (including segments) of the page being currently viewed. .. note:: Calling this function is the same as doing this: | | site_url(uri_string()); .. php:function:: uri_string() :returns: An URI string :rtype: string Returns the URI segments of any page that contains this function. For example, if your URL was this:: http://some-site.com/blog/comments/123 The function would return:: blog/comments/123 This function is an alias for ``CI_Config::uri_string()``. For more info, please see the :doc:`Config Library <../libraries/config>` documentation. .. php:function:: index_page() :returns: 'index_page' value :rtype: mixed Returns your site **index_page**, as specified in your config file. Example:: echo index_page(); .. php:function:: anchor($uri = '', $title = '', $attributes = '') :param string $uri: URI string :param string $title: Anchor title :param mixed $attributes: HTML attributes :returns: HTML hyperlink (anchor tag) :rtype: 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 :php:func:`site_url()` function above, segments can be a string or an array. .. note:: 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: My News echo anchor('news/local/123', 'My News', array('title' => 'The best news!')); // Prints: My News echo anchor('', 'Click here'); // Prints: Click Here .. php:function:: anchor_popup($uri = '', $title = '', $attributes = FALSE) :param string $uri: URI string :param string $title: Anchor title :param mixed $attributes: HTML attributes :returns: Pop-up hyperlink :rtype: string Nearly identical to the :php:func:`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 = array( '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); .. note:: 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!', array()); .. note:: The **window_name** is not really an attribute, but an argument to the JavaScript `window.open()