CodeIgniter – Basic Concepts

Codeigniter is a small footprint MVC framework that helps programmers to create robust web applications. It is considered as one of the smallest frameworks when coming to the size and learning curve.

What are the basic concepts in codeigniter ?

Controllers

Controllers are the entry point in any web application. So controller does same duty in Codeigniter as well. It is the URL that we can access via web browser.

Creating a controller

First go to application/controllers folder and create file called Movies.php. You will find two other files already there, index.html and Welcome.php, they are shipped with Codeigniter.

This Movies class will extend a class called CI_Controller, a Codeigniter core library.

Calling a controller

http://yourdomaincom/index.php/movies/index

This will print the text “hello world”. You may have noticed word movies after index.php, it is the controller name and word index is the method name. So we will call the URL like http://yourdomaincom/index.php/controller/method_name

In the next example you will see an extra method, that will be responsible for view a particular movie.

You can see the hello world example in the browser

Codeigniter Hello word controller

 

Views

Views are the real theme of the website, so it includes all the relevant html,css,js assets.  We can call the view through the controllers. Let’s create a view in the application/views folder, this is the default Codeigniter view folder. Below I have added a sample code of view in Codeigniter.

 

We can call the view through a controller as like below.

Where index is the view name, which codeigniter will find in the folder application/view/index.php, if it can’t find the file it will throw a 404 error.

If you want more cleaner approach, suppose all the movie related in one view and TV series related view in different folder you can use the below method. so this will be finding respective directories application/views/movies/index.php and  application/views/tv/index.php

Models

Models are designed to connect with database directly. For example, you have movie database, Models will be responsible for insert,update,list, and delete movie articles.

Model classes are stored in the application/models folder. See below for a sample class in Codeigniter.

So you can see Movie_model is the class name, and the file must be saved as Movie_model.php in the application/models directory.

Loading a model

Model can be called in controller. Following code can be used to load any model.

You can call a method from the model in the controller like this

 

Editorial Staff
 

Editorial Staff at tutsplanet is a dedicated team to write various tutorial articles.