CodeIgniter URLs

Next Page Previous Page

Codeigniter uses a segment-based approach, URLs in CodeIgniter are designed to be search engines rather than a standard query string for dynamic systems and user friendly. 

mycodeigniterexample.com/news/test/test_method

URI Segments:

The URL segment follows the model-view-controller approach as follows 

mycodeigniterexample.com/class/function/ID

  • Class: here class is a controller class that has to be invoked.
  • Function: function here is the method of the class that we need to call.
  • ID: ID is an additional segment that can be used to pass data in variables that goes to the controllers 

Adding a URL Suffix

When we add ur suffix it adds automatically to all of our URLs like .html, we can add suffix In config/config.php

For example, if we have example.com/index.php/suffix/view/urlsuffix

Then after we add suffix it will become  example.com/index.php/suffix/view/urlsuffix.html

Enabling Query Strings

In Query String instead of passing a path for a method, we are passing a function in the controller.

index.php?c=querystring&m=view&id=601

This can be enabled in your application/config.php file by making it true. 

$config['enable_query_strings'] = TRUE; 

$config['controller_trigger'] = 'c'; 

$config['function_trigger'] = 'm';

 

CodeIgniter provides some URL Helpers that contains the following functions:

  • site_url(); 

site_url() function will return us our site URL, which we have added in our configuration file.

Syntax: site_url([$uri = ''[, $protocol = NULL[, $altConfig = NULL]]])

Parameters:

  • $uri: Array of url segment.
  • $protocol: http or http protocols.
  • $altConfig: configuration.

Example: 

$segments = ['star', 'local', '321’'];

echo site_url($segments);

 

  • base_url();

base_url() works similar to the site_url() the only difference is in site_url() index page is appendent and in base_url() its not.

Syntax: base_url([$uri = ''[, $protocol = NULL]])

Parameters:

  • $uri: Array of url segment.
  • $protocol: http or http protocols.

Example:

echo base_url('accounts/post/450');
  • uri_string();

uir_string returns part of a URL

Syntax: uri_string();

Example:

The basecUrl: http://google.com/document/codeigniter/321

The uir_string() will return : document/codeigniter/321
  • current_url();

current_url() function will return all segment url of current page.

 

Syntax: current_url([$returnObject = false])

 

Parameters:

  • $returnObject: Only returns boolean value.

 

Example:

echo $this->uri->uri_string();

 

  • index_page();

index_page() is only used for testing till we the framework.

 

Syntax: index_page([$altConfig = NULL])

 

Parameters:

  • $altConfig: Returns the alternative configuration..

 

Example:

echo index_page();

 

  • anchor();

Just like url_site(), this function can take any segment, anchor() works as an HTML anchor tag for creating url.

 

Syntax: index_page([$altConfig = NULL])

 

Parameters:

  • $uri : URI string
  • $title: The Anchor title
  • $attributes: The HTML attributes
  • $altConfig: Alternate configuration to use

 

Example:

echo anchor('', 'Click On Anchor’);
  • anchor_popup();

Almost like an anchor() but the popup function opens the url into a new tab.

 

Syntax: anchor_popup([$uri = ''[, $title = ''[, $attributes = FALSE[, $altConfig = NULL]]]])

 

Parameters:

  • $uri : URI string 
  • $title: The Anchor title
  • $attributes: The HTML attributes
  • $altConfig: Alternate configuration to use

 

Example:

'width'       => 500,

    'height'      => 500,

    'scrollbars'  => 'yes',

    'status' => 'yes',

    'resizable'   => 'yes',

    'screenx'     => 0,

    'screeny'     => 0,

    'window_name' => '_blank'

];

echo anchor_popup('code/local/126', 'Click On Anchor!', $atts);
Next Page Previous Page

Treding Tutorial

Popular Tutorials

to Learn now