CodeIgniter Routes: Learn with Example

What are Routes?

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

Routes are defined using the formula

example.com/Controller/Method/Parameter/ 

HERE,

Let's now look at a practical example.

Consider the following URL

http://localhost:3000/contacts/edit/1

HERE,

Routes Example

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

To learn how to implement routers on a real-world project, we will assume that we are creating an application for managing contact details. The following table shows the URLs that 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 above table. We have defined the URLs, CodeIgniter route, and mapped them to the respective controller and method names.

Creating URL's for the Application

Let's create routes for our tutorial project

Open application/config/routes.php

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,

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

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's create the Contacts controller that will be responding to the actions specified in the routes.

Create a new file Contacts.php in application/controllers/Contacts.php

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,

Views

We still need to take one more step before we can test our routes in the web browser. Let's create the corresponding views to the above controller methods.

The following image shows what your application will look like

Create the following files in application/views

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

Create a new directory of contacts in views application/views/contacts

Create the following files inside

index.php
create.php
edit.php 

Your file structure should be as follows

Let's now update the header.php

<!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 following is the code for the footer.php

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

Let's 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

Summary

In this tutorial, we have learned how to create routes in CodeIgniter for a real-world example application and covered the basics of routing that you need to know to get started developing CodeIgniter.

 

YOU MIGHT LIKE: