Home >>Codeigniter Tutorial >CodeIgniter URL Routing

CodeIgniter URL Routing

CodeIgniter URL Routing

URLs in CodeIgniter are all made to be short and search engine friendly. It should make more sense to the visitors. A user should get an idea about the page content via its URL. So how to create Codeigniter routes learn here below with examples.

The functionality of routes in Codeigniter is to simplify the URL and respond with the content associated with the route and make complex URL short. A route gives back the response to the URL requested by the user. It simplifies the URL associated with the content.

For example:

www.yourdomain.com/main/home
www.yourdomain.com/main/about
www.yourdomain.com/main/contact
www.yourdomain.com/main/login
www.yourdomain.com/main/register

Routes are set after the base_url or the index_url with the controller and method crated for routing to a specific page.

How does it work?

In the browser URL, routing structure looks something like this:

domain_name.com/Controller/Method/Parameter/

In the above URL example,
Controller points to the name of the controller (class) made during the development i.e. controller defined in the controllers' folder, which then binds the controller name to the URL for the response.
Method which is defined inside the controller is invoked and bound to respond to the requested URL.
Parameter for the route is optional as it depends upon the parameters passed while making methods/functions.
**Different class methods/functions can be called instead of one corresponding to the URL**

Example:

localhost/user/delete/2
User = Controller,
Delete = Method inside User Controller,	
2 = Parameter that method takes

Setting Up Routes :

Create a new controller in application/controller [name should be the same as listed in routes.php in this case Main.php]
Make a class with the same name as the controller and inside define a method loading a view.

class Main extends CI_Controller
{
	public function home()
	{
		$this->load->view(‘home’);
	}
}

Finally create a view inside application/views folder with the name mentioned while loading inside the controller method

Application/views/home.php

Voila! You just created your route.

The above example will be result as localhost/index.php/main/home

**index.php can be removed by setting up default routes in Application/config/routes.php file**

If no matching route is found then Codeigniter throws an exception 404 Page not found.

Usage of Routes:

To build large applications with complex URLs including all routing paths along with the fetching of id makes the URL messy and is not at all user-friendly, also it is not at all Search Engine Friendly.Old convention in the URL uses query methods to pass on the request along with the extension of a page requested.

Ex:

example.com/admin/user.php?q=1

Which in response is so complex and long and for a user it makes no sense. Users should get an idea of the content present on the page through its URL.

The easier way it can be done is

example.com/admin/user/1

which makes more sense about the page associated with it. Using routes URL becomes cleaner and increase the user experience in routing the web. Moreover, URL routing helps in creating routes that are SEO friendly for reaching the page with a quick response.

Custom Routes for Application:

As we learned about how routes work in Codeigniter. Let’s now create some custom URL’s for our application so that we will be able to implement it in our real-world projects. We will be implementing routes in a small blog app where we can create, update, delete, and see blog views based on different routes in URL.

For each function to work the controller will be the same i.e. blog and methods inside are different. The working structure of this routing will work something like this shown below:

/blog : shows the list of all blogs
/blog/create : shows create view for creating a blog
/blog/update/id : shows the update view with specific id
/blog/delete/id : shows the delete view with specific id
Above URL will work on the following methods called inside the controller:
/blog 		= index method
/blog/create 	= create method
/blog/update	= update method
/blog/delete 	= delete method

Before creating a controller and view for the above structure to work. Custom Url must be setup by creating routes for the project. Open application/config/route.php Add specific lines omitting the default one.

$route[‘default_controller’] 		= ‘welcome’;
$route[‘blog’] 				= ‘blog’;
$route[‘blog/create’]			= ‘blog/create’;
$route[‘blog/update/:id’]		= ‘blog/update’;
$route[‘blog/delete/:id’]		= ‘blog/delete’;

Explanation :

The lines on the right of equals sign are controllers and controller pointing to the method they are calling and the lines on the left side of equals sign defines its route where it is going or the url which is shown.
Therefore, in our scenario

$route[‘blog’] = ‘blog’; Showing the blog route pointing to the blog controller which calls the index method inside controller.
$route[‘blog/create’] = ‘blog/create’; Showing the blog/create route pointing the blog controller to call create method.
$route[‘blog/update/:id] = ‘blog/update; Showing the blog/update/{id} route pointing the blog controller to call update method.
$route[‘blog/delete/:id] = ‘blog/delete; Showing the blog/delete/{id} route pointing the blog controller to call delete method.

After all the routes are set, make a controller for the blog that will respond to the routes specified above. Inside application/controllers make new file Blog.php and following code:

  <?php
	class Blog extends CI_Controller
	{
	  public function __construct()
		{
		parent::__construct();
		$this->load->helper(‘url’);
		}
		public function index()
		{
		$this->load->view(‘index’);
		}
		public function create()
		{
		$this->load->view(‘create’);
		}
		public function update($id)
		{
		$this->load->view(‘update’);
		}
		public function delete($id)
		{
		$this->load->view(‘delete’);
		}
	}
  ?>

Here Blog controller defines all the methods we mentioned in the routes i.e. index, create, update, delete
Now just create views for all specific methods inside applications/views/
- index.php
- create.php
- update.php
- delete.php
And echo out custom message on different page.
Inside update.php and delete.php echo $id; to see the id which you pass as the parameter in the URL.
Open up the browser and try the following

localhost/blog
localhost/blog/create
localhost/blog/update/1
localhost/blog/delete/1

No Sidebar ads