Codeigniter URL Helper


Codeigniter URL Helper – Codeigniter URL Helper contains functions That is used for parsing the URL And break Down Into Several Segment and pass to the Controller Or store as a Variable.$this->load->helper(‘url’); is used to load the helper. Here in this tutorial, we are going to explain how to use URL helper in CodeIgniter.


Codeigniter URL Helper Example

Let us first see how to load URL helper in codeigniter and then use its function-

Load URL Helper

How To Load URL Helper in Codeingniter Example:

 
$this->load->helper('url');

Functions:-

There are many functions are available in URL helper. Now we will explain one by one with example.

    • 1. Site URL, Base URL, The current URL
    • 2. An URI string
    • 3. index_page value
    • 4. hyperlink (anchor tag)
    • 5. Pop-up hyperlink
    • 6. A “mail to” hyperlink and safe “mail to” hyperlink
    • 7. Linkified string
    • 8. URL-formatted string
    • 9. Protocol-prefixed URL string
    • 10. redirect

    1. Site URL, Base URL, The current URL

    This Function is used to work with url like Site, Base and Current url:-

    Syntax of Site URL, Base URL, The current URL function is:-

    site_url([$uri = ''[$protocol = NULL]])
    base_url($uri = '', $protocol = NULL)	
    current_url()
    

      Parameters:

    • $uri (string) : URI string
    • Return type : string

    EXAMPLE

    Here is simple example of Site URL, Base URL, The current URL.

    Site URL, Base URL, The current URL in Codeigniter Example:-

    //Controllers part
    public function urlCheck()
    	{
    		$this->load->helper('url');
    		$this->load->view('url_view');
    	}
    
    // Views parts
    <h3>Site Url</h3>
    <?php
    $path = array('user', 'file', 'url');
    echo site_url($path);
    ?>
    <br>
    <br>
    <h3>Base Url</h3>
    <?php
    echo base_url("images/icons/edit.png");
    ?>
    <br>
    <br>
    <h3>Current Url</h3>
    <?php
    echo current_url()
    ?>
    

    The output will be like this –

    2. An URI string

    This Function is used to redirect directory path . Syntax of An URI string function is:-

    Syntax of An URI string function is:-

    uri_string()
    

      Parameters:

    • $uri (string) : URI string
    • Returns : uri string
    • Return type : string

    EXAMPLE

    Here is simple example of An URI string.

    An URI string in Codeigniter Example:-

    
    
    // Views parts
    <?php
    echo uri_string()
    ?>
    

    This Helper return only uri string

    The output will be like this –

    3. index_page value

    Syntax of index_page value function is:-

    Syntax of index_page value function is:-

    index_page()
    

      Parameters:

    • Returns : index_page value
    • Return type : mixed

    EXAMPLE

    Here is simple example of index_page value.

    index_page value in Codeigniter Example:-

    // Views parts
    <?php
    echo index_page();
    ?>
    

    The output will be like this –

    4. hyperlink (anchor tag)

    This Function is used to create standard html anchor link based on your local url. It accept three parameters-Uri strings, title and attributes:-

    Syntax of hyperlink (anchor tag) function is:-

    anchor($uri = '', $title = '', $attributes = '')
    

      Parameters:

    • $uri (string) : URI string
    • $title (string) : Anchor title
    • $attributes (mixed) : HTML attributes
    • Returns : HTML hyperlink (anchor tag)
    • Return type : string

    EXAMPLE

    Here is simple example of hyperlink (anchor tag).

    hyperlink (anchor tag) in Codeigniter Example:-

    // Views parts
    <?php
    echo anchor('news/local/123', 'My News', 'title="News title"');
    ?>
    

    The output will be like this –

    5. Pop-up hyperlink

    Pop-up Hyperlink function is used to create html anchor link on new window. Syntax of Pop-up hyperlink function is:-

    Syntax of Pop-up hyperlink function is:-

    anchor_popup($uri = '', $title = '', $attributes = FALSE)
    

      Parameters:

    • $uri (string) : URI string
    • $title (string) : Anchor title
    • $attributes (mixed) : HTML attributes
    • Returns : Pop-up hyperlink
    • Return type : string

    EXAMPLE

    Here is simple example of Pop-up hyperlink.

    Pop-up hyperlink in Codeigniter Example:-

    // Views parts
    <?php
    $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);
    ?>
    

    The output will be like this –

    6. A “mail to” hyperlink and safe “mail to” hyperlink

    This function is used to Create link on mail by these functions. It Has three parameters- email string, Title and attributes. Syntax of A “mail to” hyperlink and safe “mail to” hyperlink function is:-

    Syntax of A “mail to” hyperlink and safe “mail to” hyperlink function is:-

    mailto($email, $title = '', $attributes = '')
    safe_mailto($email, $title = '', $attributes = '')
    

      Parameters:

    • $email (string) : E-mail address
    • $title (string) : Anchor title
    • $attributes (mixed) : HTML attributes
    • Returns : A “mail to” hyperlink and A spam-safe “mail to” hyperlink
    • Return type : string

    EXAMPLE

    Here is simple example of A “mail to” hyperlink and safe “mail to” hyperlink.

    A “mail to” hyperlink and safe “mail to” hyperlink in Codeigniter Example:-

    // Views parts
    <?php
    $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);
    ?>
    

    The output will be like this –

    Codeigniter URL Helper

    7. Linkified string

    This function is used to create link of email or url. It accepts three parameters- input string, type and popup. Syntax of Linkified string function is :-

    Syntax of Linkified string function is:-

    auto_link($str, $type = 'both', $popup = FALSE)
    

      Parameters:

    • $str (string) : Input string
    • $type (string) : Link type (‘email’, ‘url’ or ‘both’)
    • $popup (bool) : Whether to create popup links
    • Returns : Linkified string
    • Return type : string

    EXAMPLE

    Here is simple example of Linkified string.

    Linkified string in Codeigniter Example:-

    // Views parts
    <?php
    $string = "sonukr321@gmail.com";
    $string = auto_link($string, 'both', TRUE);
    echo $string;
    ?>
    

    The output will be like this –

    Codeigniter URL Helper

    8. URL-formatted string

    This Function is used to create a human friendly url strings. it accept three parameter-input string, seperetor(this is second parameter which is used to seperated a word) and lowercase (bool). Syntax of URL-formatted string function is:-

    Syntax of URL-formatted string function is:-

    url_title($str, $separator = '-', $lowercase = FALSE)
    

      Parameters:

    • $str (string) : Input string
    • $separator (string) : Word separator
    • $lowercase (bool) : Whether to transform the output string to lower-case
    • Returns : URL-formatted string
    • Return type : string

    EXAMPLE

    Here is simple example of URL-formatted string.

    URL-formatted string in Codeigniter Example:-

    // Views parts
    <?php
    $title = "What's wrong with CSS?";
    $url_title = url_title($title);
    echo $url_title;
    ?>
    <br>
    <?php
    $title = "What's wrong with CSS?";
    $url_title = url_title($title, 'underscore', TRUE);
    echo $url_title;
    ?>
    

    The output will be like this –

    Codeigniter URL Helper

    9. Protocol-prefixed URL string

    This Function is used to added http tag in url and pass the url string to the function. Syntax of Protocol-prefixed URL string function is:-

    Syntax of Protocol-prefixed URL string function is:-

    prep_url($str = '')
    

      Parameters:

    • $str (string) : url string
    • Returns : Protocol-prefixed URL string
    • Return type : string

    EXAMPLE

    Here is simple example of Protocol-prefixed URL string.

    Protocol-prefixed URL string in Codeigniter Example:-

    // Views parts
    <?php
    $url = prep_url('www.tutorialsplane.com');
    echo $url;
    ?>
    
    

    The output will be like this –

    Codeigniter URL Helper

    10. Redirect

    This Function is used to redirect url with base url. this function accept three parameters-uri strings, methods and code.Syntax of Redirect function is:-

    Syntax of Redirect function is:-

    redirect($uri = '', $method = 'auto', $code = NULL)
    

      Parameters:

    • $uri (string) : URI string
    • $method (string) : Redirect method (‘auto’, ‘location’ or ‘refresh’)
    • $code (string) : HTTP Response code (usually 302 or 303)
    • Return type : void

    EXAMPLE

    Here is simple example of Redirect.

    Redirect in Codeigniter Example:-

    // Views parts
    <?php
    if ($logged_in == FALSE)
    {
            redirect('/login/form/');
    }
    
    redirect('/article/13', 'location', 301);
    ?>
    
    

    The output will be like this –


    Advertisements

    Add Comment

    📖 Read More