CodeIgniter Routes: URL Routing with Example

⚡ Smart Summary

CodeIgniter URL Routing maps a URL to a controller and method, with an optional parameter. Routes are defined in routes.php, so a clean URL like contacts/edit/1 runs the edit method of the Contacts controller, which loads header, content, and footer views.

  • 🧭 Core Rule: A route matches a URL to a controller, method, and optional parameter.
  • 📐 Formula: The pattern is Controller/Method/Parameter, defined in application/config/routes.php.
  • 🏠 Default Route: default_controller sets the home page when no URL segment is given.
  • 🔢 Parameters: A route such as edit/:id passes the id value into the controller method.
  • 🎛️ Controller: Each route maps to a method that loads the matching views.
  • 🖼️ Shared Views: Header and footer views are reused, with a specific middle view per action.
  • 404 Handling: An unmatched URL raises a page-not-found exception, overridable with 404_override.

CodeIgniter Routes

What are CodeIgniter Routes?

Routes are responsible for responding to URL requests. Routing matches the URL to the pre-defined routes. If no route match is found, CodeIgniter throws a page-not-found exception.

Routes in CodeIgniter are defined using the formula below:

example.com/Controller/Method/Parameter/

Here:

  • Controller is mapped to the controller name that should respond to the URL.
  • Method is mapped to the method in the controller that should respond to the URI request.
  • Parameter – this section is optional.

CodeIgniter Routes Example

Let us now look at a practical URL routing in CodeIgniter example. Consider the following URL: http://localhost:3000/contacts/edit/1

Here:

  • The name of the controller responding to the above URL is “contacts”.
  • The method in the controller class Contacts is “edit”.
  • The edit method accepts a parameter. In our example, the value “1” is passed to the method.

Here is a brief background of what we plan to do:

  • Routing – routing responds to URL requests. CodeIgniter routing matches the URL to the pre-defined routes. If no route match is found, CodeIgniter throws a page-not-found exception.
  • Controllers – routes are linked to controllers. Controllers glue the models and views together. They request data and business logic from the model and return the results via the view’s presentation. Once a URL has been matched to a route, it is forwarded to a controller public function that interacts with the data source and business logic and returns the view.
  • Views – views are responsible for presentation. A view is usually a combination of HTML, CSS, and JavaScript. This part is responsible for displaying the web page to the user. Typically, the data displayed is retrieved from the database or another data source.

To learn how to implement routes in a real-world project, we assume that we are creating an application for managing contact details. The following table shows the URLs we will be working with.

S/N URL Route Controller Method
1 / $route[‘default_controller’] Welcome index
2 /contacts $route[‘contacts’] Contacts index
3 /contacts/create $route[‘create’] Contacts create
4 /contacts/edit/id $route[‘edit/:id’] Contacts edit
5 /contacts/update/id $route[‘update/:id’] Contacts update
6 /contacts/delete/id $route[‘delete/:id’] Contacts delete

We will create the routes of our application based on the table above. We have defined the URLs, the CodeIgniter route, and mapped them to the respective controller and method names.

Creating URL Routing for the Application

Let us create CodeIgniter URL routing for our tutorial project. Open application/config/routes.php and modify the routes to match the following:

$route['default_controller'] = 'welcome';
$route['contacts'] = 'contacts';
$route['create'] = 'contacts/create';
$route['edit/:id'] = 'contacts/edit';
$route['update/:id'] = 'contacts/update';
$route['delete/:id'] = 'contacts/delete';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

Here:

  • $route[‘default_controller’] = ‘welcome’; defines the default controller Welcome.
  • $route[‘contacts’] = ‘contacts’; defines a contacts route that calls the index method in the Contacts controller.
  • $route[‘create’] = ‘contacts/create’; defines a route create that points to the Contacts controller and calls the create method.
  • $route[‘edit/:id’] = ‘contacts/edit’; defines a route edit that accepts a parameter of id and points to the edit method of the Contacts controller.
  • $route[‘update/:id’] = ‘contacts/update’; defines a route update that accepts a parameter of id and points to the update method of the Contacts class.
  • $route[‘delete/:id’] = ‘contacts/delete’; defines a route delete that accepts a parameter of id and points to the delete method of the Contacts controller.

The following table shows the respective URLs derived from the routes defined above:

S/N Route Corresponding URL
1 $route[‘default_controller’] = ‘welcome’; http://localhost:3000
2 $route[‘contacts’] = ‘contacts’; http://localhost:3000/contacts
3 $route[‘create’] = ‘contacts/create’; http://localhost:3000/contacts/create
4 $route[‘edit/:id’] = ‘contacts/edit’; http://localhost:3000/contacts/edit/1
5 $route[‘update/:id’] = ‘contacts/update’; http://localhost:3000/contacts/update/1
6 $route[‘delete/:id’] = ‘contacts/delete’; http://localhost:3000/contacts/delete/1

Now that we have covered the routes, let us create the Contacts controller that will respond to the actions specified in the routes. Create a new file Contacts.php in application/controllers/Contacts.php and add the following code:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Contacts extends CI_Controller {

public function __construct() {
parent::__construct();
$this->load->helper('url');
}

public function index() {
$this->load->view('header');
$this->load->view('contacts/index');
$this->load->view('footer');
}

public function create() {
$this->load->view('header');
$this->load->view('contacts/create');
$this->load->view('footer');
}

public function edit($id) {
$this->load->view('header');
$this->load->view('contacts/edit');
$this->load->view('footer');
}

public function update($id) {
$this->load->view('header');
$this->load->view('contacts/update');
$this->load->view('footer');
}

public function delete($id) {
$this->load->view('header');
$this->load->view('contacts/delete');
$this->load->view('footer');
}
}

Here:

  • class Contacts extends CI_Controller {…} defines our controller class and extends the CI_Controller class that comes with CodeIgniter.
  • The methods defined above correspond to the routes we defined, and those with parameters like delete accept a parameter of $id.
  • Notice the functions load three views. The header and footer are common for all methods. The middle view is specific to the action, i.e. delete for the delete function and create for the create function. Another important thing to remember is that the views are loaded from the contacts subdirectory.

CodeIgniter Views

We still need one more step before we can test our CodeIgniter routes with parameters in the web browser. Let us create the views corresponding to the controller methods above. The following image shows what the application will look like:

CodeIgniter contacts application preview

Create the following files in application/views:

header.php – this file will contain the contacts app menu and the header
footer.php – this file will contain the application footer

Create a new directory contacts inside application/views (application/views/contacts) and create the following files inside it:

index.php
create.php
edit.php

Your file structure should be as follows:

CodeIgniter views file structure

Let us now update the header.php file:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CodeIgniter Routes</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.2/css/bulma.min.css">
<script defer src="https://use.fontawesome.com/releases/v5.3.1/js/all.js"></script>
</head>
<body>
<section class="section">
<div class="container">
<h1 class="title">CI Contacts v1</h1>
<h2 class="subtitle">CodeIgniter contacts management app</h2>
<div class="columns">
<div class="column is-one-quarter">
<aside class="menu">
<p class="menu-label">
General
</p>
<ul class="menu-list">
<li><a class="is-active" href="#">Dashboard</a></li>
<li><a href="<?=site_url('contacts/create')?>">New Contact</a></li>
<li><a href="<?=site_url('contacts/edit/1')?>">Edit Contacts</a></li>
</ul>
<p class="menu-label">
Settings
</p>
<ul class="menu-list">
<li><a href="#">SMS</a></li>
<li><a href="#">Email</a></li>
</ul>
</aside>
</div>

Here:

  • The HTML code above loads Bulma CSS from a CDN.

The following is the code for the footer.php file:

</div>
</div>
</section>
</body>
</html>

Let us now add the code for the index.php, edit.php, and create.php files for contacts:

index.php
<div class="column">Index content goes here...</div>
edit.php
<div class="column">Edit content goes here...</div>
create.php
<div class="column">Create content goes here...</div>

You can save all the changes that have been made. Open the following URL in your web browser: http://localhost:3000/contacts/. You can click on the New Contact and Edit Contact links and see what happens.

FAQs

Define the route with a placeholder such as edit/:id, and accept the matching argument in the controller method, for example edit($id). CodeIgniter passes the URL segment into that argument.

CodeIgniter throws a page-not-found exception and shows the 404 page. You can point 404_override at your own controller to display a custom error page instead.

site_url() builds a full URL from your base_url and the route, so links keep working if the site moves or the base path changes. It needs the URL helper to be loaded first.

Yes. From a resource name like contacts, AI can produce the index, create, edit, update, and delete routes with matching controller methods, following REST conventions. Confirm the parameter names.

Loading header and footer separately avoids repeating the same layout in every page view. Each method loads the shared header, a specific content view, then the shared footer.

Summarize this post with: