Step 1.1: Again about MVC and CodeIgniter – The controllers

(created: January 19, 2015; last update: -)

We’ve installed a fresh copy of CodeIgniter. But what about MVC? Where is the MVC?

For the moment we will only work inside the application directory. Of course you can also look and/or (DON’T) change files inside the system directory.

The application directory is, as its name implies, the directory where you develop your application in CodeIgniter.

If you look at the directory structure of your CodeIgniter application you will see something like the following (I will only mention the important parts for this step):

-application/
- -config/
- -controllers/
- -models/
- -views/

There you go: M(odels)V(iews)C(ontrollers).

The controllers

The controllers are the decisional part of your MVC. They decide what to do on user interaction. For example, when a user clicks on a link or uses a form those actions are handled by the controller which decides if will question the database with the help of a model (or maybe it won’t question the database but can put the model to work for it…), and with the data received will load a view that will output the data.

CodeIgniter comes with an example controller which is actually a class. Its name is Welcome and it’s defined inside the application/controllers/Welcome.php.

As you can see, it has an index() method that will simply output a view. The index() method is the default method that is called when someone uses only the controller’s name as URL. So, when you are going to http://localhost/welcome, CodeIgniter will automatically load the index() method.

(Is worth mentioning that if you are visiting http://localhost/ CodeIgniter will also serve you the index() method of the Welcome controller. That is happening because of a setting that can be found inside the application/config/routes.php. If you want another controller to be called when the “homepage” is called, you should change the $route[‘default_controller’] value to the controller you want to be served.)

So, a Controller is a class that can be found inside application/controllers. Is important to note that the name of the file is the same as the name of the controller/class, and starts with a big letter. Also, if your class/controller name is made of two or more words, the words should be separated with an underscore. If, for example, we have an “About us” page, the name of the controller should be “About_us”, and the name of the file should be “About_us.php”.

Looking at the “Welcome” page, you can see how a “template” (or, rather, a skeleton) of a controller should look like:

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

class Welcome extends CI_Controller {

  public function index()
  {
    $this->load->view('welcome_message');
  }
}

First of all, we should make sure from the first line of code that the controller is not called without the bootstrapper (the index.php that calls the application). We do this by making sure that the BASEPATH constant is already defined.

Also, see how the view is called. $this->load->view(‘welcome_message’);  tells our application that it should load a view named ‘welcome_message.php‘ that can be found inside the application/views directory.

“But how can this be dynamic?”, you may ask. Well… is not yet, but let’s try, for example, to create a title for our page.

Instead of just calling the view, we first define our page title. Considering that inside a page we may have a lot of variables, we are defining all variables inside an array. So, to define a page title we would do something like this:

$data['page_title'] = 'Our first CodeIgniter application';

Now we only have to pass the variables to our view we do this by adding the $data array as a second parameter to the $this->load->view() method. So our controller would look like this:

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

class Welcome extends CI_Controller {

  public function index()
  {
    $data['page_title'] = 'Our first CodeIgniter application';
    $this->load->view('welcome_message', $data);
  }
}

In the next lesson we will see how the view looks like.

6 comments

  1. If anyone else (like me) is using apache virtual hosts and had some issues accessing the welcome controller directly after the domain like so, http://avenir3steps.dev/controller

    I thought I had to set the base_url like so

    $config[‘base_url’] = ‘http://avenir3steps.dev/’;

    but that didn’t work so I had to stick http://avenir3steps.dev/index.php/controller to get it working. I read something about why this was a while back when I first checked out CI, but I guess I’ll figure it out later.

    For now, I’m using php -S localhost:1234 which allows me to access localhost:1234/welcome

  2. very good tutorial Avenir.
    step by step explaining is the best way to understand any new technology.
    keep it up, and we’ll keep it studying and learning.

Leave a Reply

Your email address will not be published. Required fields are marked *

No spam? * Time limit is exhausted. Please reload CAPTCHA.