A route is a way to remapping, makes more meaningful and SEO friendly URLs. The visitor views the newly generated page URL and the route calls the defined action URL which will be hidden from the visitors.
In CodeIgniter, a URL consists of controller-name/method-name but you can manipulate it by declaring routing.
All routed actions will be written on the specific file.
Contents
1. Download
- Download the Codeigniter from here.
- Extract zip file on the
htdocsfolder and rename the folder e.g. codeigniter. - Open
application/config/config.phpfile. - Update the
$config['base_url']value.
$config['base_url'] = 'http://localhost/codeigniter/';
2. Controller
I am defining some of the methods that use while defining the route.
- Navigate to
application/controllers/directory. - Open the
Welcome.phpcontroller file. - Define some methods – index(), admin(), showList(), updateList(), deleteList(). I created Links() methods to display the links.
Completed Code
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index() {
echo "index";
self::Links();
}
public function showList(){
echo "show List";
self::Links();
}
public function admin(){
echo "admin";
self::Links();
}
public function deleteList($delid=0){
echo "Delete List : ".$delid;
self::Links();
}
public function updateList($name="",$updateid = 0){
echo "name : ".$name;
echo "update id : ".$updateid."<br>";
self::Links();
}
public function Links(){
$this->load->helper('url');
echo "<br><br>
<a href='".base_url()."'>Index</a><br>
<a href='".base_url()."index.php/welcome/list'>Show</a><br>
<a href='".base_url()."index.php/admin'>Admin</a><br>
<a href='".base_url()."index.php/welcome/delete/12'>Delete</a><br>
<a href='".base_url()."index.php/welcome/update/sonarika/3'>Update</a><br> ";
}
}
Above created controller methods normally access like – localhost/codeigniter/index.php/welcome/showList where it consists of index.php/controller-name/method-name but I will manipulate it by defining routing in route.php file.
3. Route
All routing is defined in route.php file.
- Navigate to your project.
- Open
application/config/routes.phpfile.
$route['welcome/list'] = 'welcome/showList/'; $route['admin'] = 'welcome/admin/';
I have defined two values in $route array variable which consist of the key and a value. The key is matched in the URL if is found then re-route to the value.
- I changed the displayed method name from
showListtolist. Now the URL to access is –localhost/codeigniter/index.php/welcome/list. NOTE – There is nolistname method is been declared inWelcome.phpcontroller file. - I defined a single key
adminwhich does not consist ofcontroller-name/method-namepattern and it is pointing towelcome/admin/. Now the access URL islocalhost/codeigniter/index.php/admin/.
4. Wildcard
It is mainly used when creating a URL related to the parameter.
There are two types of wildcards –
- :num – match number on the URL if its find any other type of values like – string then it will show 404 Page not found error.
- : any – match any type of value either string or number.
$route['welcome/delete/(:num)'] = 'welcome/deleteList/$1'; $route['welcome/update/(:any)/(:num)'] = 'welcome/updateList/$1/$2';
Defined two routes –
- I used
:numwildcard in Array key and on value-added$1to access the value in the controller. - Used
:anyand:numwildcard in Array key and on value-added$1/$2for access to the controller.
5. Conclusion
With routing, you can either hide controller-name, method-name, or both of them and display the new URL to the visitor.
Use the wildcard rules according to your requirements while defining the routing.
If you found this tutorial helpful then don't forget to share.
