Well... I'm out of ideas on what to write about. Do you have any ideas? Please, write me in any comment section you find, and I will do my best to tell you about that particular subject

Step 1.1: Again about MVC and CodeIgniter – The controllers

Created at: January 19, 2015; Last update: January 21, 2015

(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):

INI

1

2

3

4

5

-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:

Welcome.php

PHP

1

2

3

4

5

6

7

8

9

<?phpdefined('BASEPATH')ORexit('No direct script access allowed');

classWelcomeextendsCI_Controller{

publicfunctionindex()

{

$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:

PHP

1

$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: