How to route a URL in CodeIgniter?
URL Routing
Codeigniter provide URLs in CodeIgniter are designed to be SEO(search-engine) and human friendly.
https://ittutorial.in/blog/blog_details/advantages-and-drawback-of-php-language
Note : Query string URLs can be optionally enabled, as described below.
URI Segments
CodeIgniter uses a segment-based approach. The segments in the URL, in following with the Model-View-Controller approach, usually represent:
http://ittutorial.in/controller-name/function-name/ID
The first segment represents the controller class that should be invoked.
The second segment represents the class function, or method, that should be called.
The third, and any additional segments, represent the ID and any variables that will be passed to the controller.
The URI Library and the URL Helper contain functions that make it easy to work with your URI data. In addition, your URLs can be remapped using the URI Routing feature for more flexibility.
Adding a URL Suffix
In your config/config.php file you can specify a suffix that will be added to all URLs generated by CodeIgniter. Ex:
https://ittutorial.in/user_guide/general/urls
You can optionally add a suffix, like .html, making the page appear to be of a certain type:
https://ittutorial.in/user_guide/general/urls.html
$config['url_suffix'] = '';
Enabling Query Strings
In some cases you might prefer to use query strings URLs:
index.php?c=products&m=view&id=345
CodeIgniter optionally supports this capability, which can be enabled in your application/config.php file. If you open your config file you’ll see these items:
$config['enable_query_strings'] = FALSE;
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';
If you change “enable_query_strings” to TRUE this feature will become active. Your controllers and functions will then be accessible using the “trigger” words you’ve set to invoke your controllers and methods:
index.php?c=controller&m=method
Note :
If you are using query strings you will have to build your own URLs, rather than utilizing the URL helpers (and other helpers that generate URLs, like some of the form helpers) as these are designed to work with segment based URLs.
Basic URL structure
http://ittutorial.in/class_name/function_name/id
class represents controller class that needs to be invoked.
function is the method that is called.
ID is any additional segment that is passed to controllers.
What is site_url();
You can pass a string or an array in a site_url() function. In this example we'll pass a string,
echo site_url('blog/ittutorial_blog/mytopic');
The above function will return something like this
http://ittutorial.in/index.php/blog/ittutorial_blog/mytopic
In this example we'll pass an array,
$data = array('blog', 'ittutorial_blog', 'mytopic');
echo site_url($data);
What is base_url();
It returns your site base URL, if mentioned any, in the config file. On passing base_url(), it also returns the same thing as site_url() with eliminating index.php. This is useful because here you can also pass images or text files. Here also you can pass a string or an array.
In this example we'll pass a string,
echo base_url("book/novel/fiction");
The above function will return something like this http://ittutorial.in/ book/novel/fiction
In order to use base_url(), you must first have the URL Helper loaded. This can be done either in application/config/autoload.php
$autoload['helper'] = array('url');
Or, manually:
$this->load->helper('url');
Once it's loaded, be sure to keep in mind that base_url() doesn't implicitly print or echo out anything, rather it returns the value to be printed:
echo base_url();
Remember also that the value returned is the site's base url as provided in the config file. CodeIgniter will accomodate an empty value in the config file as well:
If this (base_url) is not set then CodeIgniter will guess the protocol, domain and path to your installation.
application/config/config.php, line 13
If you want to use base_url(), so we need to load url helper.
By using autoload $autoload['helper'] = array('url');
Or by manually load in controller or in view $this->load->helper('url');
Then you can user base_url() anywhere in controller or view.
what is difference between site_url() and base_url().
echo base_url(); // http://example.com/website
echo site_url(); // http://example.com/website/index.php
if you want a URL access to a resource (such as css, js, image), use base_url(), otherwise, site_url() is better.
The purpose of site_url is that your pages become more portable in the event your URL changes.The site_url appears with the index.php file.
Segments can be optionally passed to the function as a string or an array.
echo site_url("news/local/123");
it will give: http://ci.com/index.php/news/local/123
you can even pass segments as an array:
$segments = array('news', 'local', '123');
echo site_url($segments);
base_url is without the index_page or url_suffix being appended. like site_url, you can supply segments as a string or an array.
If you want a URL access to a resource use base_url you can supply a string to a file, such as an image or stylesheet else site_url is enough.
echo base_url("/images/icons/image.png");
What is uri_string();
It returns the URI segment of a page. For example, if your URL is,
http://ittutorial.in/book/novel/fiction
Then, uri_string() will return
Book/novel/fiction
What is current_url();
Calling this function means, it will return the full URL of the page currently viewed.
Please note -> calling this function is same as calling uri_string() in site_url().
current_url() = site_url(uri_string());
What is index_page();
It will return your site's index_page which you have mentioned in your config file. By default, it is always index.php file.
You can change it with the help of .htaccess file.
what is anchor() method.
It creates a standard HTML link based on your local site URL. For example,
Echo anchor('book/novel/fiction', 'my itechtuto', 'title="book name");
It will give the following result,
echo anchor('controller/function/parameter', 'Link Text');
//
Link Text
anchor tag with additional attributes.
echo anchor('controller/function/parameter', 'Link with Title attribute',array('title'=>'Link Title'));
// <a href="http://example.com/index.php/controller/function/parameter" title="Link Title">Link with Title attribute</a>
anchor_popup()
It is identical to anchor() but it opens the URL in a new window.
Below examples show links with site_url() function
<a href="<?php echo site_url('controller/function/parameter'); ?>">Link Title</a>
//<a href="http://example.com/index.php/controller/function/parameter">Link Title</a>
<a href="<?php echo site_url('controller/function/parameter1'); ?>/parameter2">Link Title</a>
//<a href="http://example.com/index.php/controller/function/parameter1/parameter2">Link Title</a>
<a href="<?php echo site_url('controller/function/parameter'); ?>" title="title attributes ">Link Title</a>
// <a href="http://example.com/index.php/controller/function/parameter" title="title attributes">Link with Title attribute</a>
mailto()
It creates a HTML email link. For example,
Echo mailto('info@ittutorial.in', 'To contact me click here')
url_title()
It takes a string as an input and creates a human friendly environment. For example,
$title = "CodeIgniter's examples"
$url_title() = url_title($title);
Output will be "CodeIgniters-examples"
If you'll pass a second parameter, it defines word delimiter.
$title = "CodeIgniter's examples"
$url_title() = url_title($title, '_');
Output will be "CodeIgniters_examples"
If you'll pass a third parameter, it defines uppercase and lowercase. You have Boolean options for this, TRUE/FALSE.
$title = "CodeIgniter's examples"
$url_title() = url_title($title, '_', TRUE);
Output will be "codeigniters_examples"
NOTE :-
site_url() : 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, and the url_suffix as set in your config file.
base_url() : Returns your site base URL, as specified in your config file,This function returns the same thing as site_url, without the index_page or url_suffix being appended.This method is suitable for creating Style Sheet links , Script links and image links.
anchor() : anchor() function create the links,[anchor(uri segments, text, attributes)] 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.
How to get current url in codeigniter.
File Name :
<?php
defined('BASEPATH') OR exit('no direct script access allowed');
class Current_url_get extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
// How to get current page URL in Codeigniter?
//If url helper is loaded, use current_url();
$page_url=current_url();
//echo $page_url;
//echo base_url(uri_string());
echo $this->uri->uri_string();
//echo $this->router->fetch_class(); //It will returns controller name
//echo "<br/>";
//echo $this->router->fetch_method(); //It will return current method name.
}
public function current_user()
{
// How to get current page URL in Codeigniter?
$page_url=current_url();
echo $page_url;
}
}
How to Get Current Url in php
// for both https and http
// $actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$currentURL = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
How to get previous page url in codeigniter.
we may sometime require to redirect previous page after login then you can get previous page path by using codeigniter user_agent library, request array or using session.
File Name :
<?php
defined('BASEPATH') OR exit('no direct script access allowed');
class Previousurlget extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('admin_views/previousurlget');
}
}
<html>
<head>
</head>
<body>
<a href="<?php echo base_url()?>admin/nexturl">Go</a>
</body>
</html>
<?php
defined('BASEPATH') OR exit('no direct script access allowed');
class Nexturl extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
// 1st method .....................
$this->load->library('user_agent');
if ($this->agent->is_referral())
{
$refer = $this->agent->referrer();
echo $refer;
}
// 2nd method...................
/* $url= $_SERVER['HTTP_REFERER'];
echo $url;
redirect($url);
*/
// 3rd method...................
$refer = $this->agent->referrer();
echo $refer;
redirect($this->agent->referrer());
// 4th method ....................
//$previous_url = $this->session->userdata('previous_url');
//print_r($previous_url);
}
}
Previous
Next