Codeigniter for the beginner
I have seen in some forums and different websites, some doubts about how codeigniter is organized and how can it help you as a coder be more efficient. The first thing people should know is, codeigniter is a PHP framework, made with the software architecture known as MVC which stands for Model-View-Controller. This architecture seperates the logic from user interface allowing independant development, testing and better maintenance. Codeigniter allows you to have your code much organized, making you look better at what you already do. This framework is made to facilitate the way you do certain things in PHP, and allow you to reuse your code, using PHP Object Oriented programming paradigm. Codeigniter, on my opinion, seems to be very user friendly, so it’s to which I would recommend the use of it, for any upcoming web developer.
How to install Codeigniter
Some people start to doubt about the use of codeigniter when they hear or read the word ‘install’. In this case, you shouldn’t, the install process for codeigniter is farily simple, since all you need to do is, download the framework from their website, which can be found here: http://codeigniter.com/
Direct download is here: Latest Version
Next, after you have downloaded the files, just extract the content and place them into your root folder. That’s it! You can also rename your folder as you wish, so I suggest giving it a relevant name.
Config File
This config file can be found in root folder -> application -> config -> config.php. For this post and simple example, all we would need to do is change one line of code:
PHP
<?php
$config['base_url'] = 'http://localhost/ci_basics';
?>
<?php
$config['base_url'] = 'http://localhost/ci_basics';
?>
In my case, I have put my base_url as localhost/ci_basics. This tells codeigniter that the files for this project are in the folder named ci_basics. This should be all for now, since this example is very simple, and it’s just to let all of us know how simple codeigniter can be.
Routes File
The next thing that we should change is our routes file. This file can also be found in the same path as our config file, root folder -> application -> config -> routes.php. In this file, we would change our default_controller. Since we are going to name our first page ‘home’, and also, name our first controller home, then we can tell codeigniter where to look first.
PHP
<?php
$route['default_controller'] = 'home';
?>
<?php
$route['default_controller'] = 'home';
?>
That’s enough configuration for today. Now let’s create our home controller. Controllers are just PHP classes that are within the folder controller. Each class should have the same name as it’s file. Also, each class will extend from codeigniter’s main controller. Let’s see how.
File name home.php
PHP
<?php
// This file name is home.php
// The class name for this file should be the same
// filename, except it should be capitalized.
class Home extends CI_Controller{
}
?>
<?php
// This file name is home.php
// The class name for this file should be the same
// filename, except it should be capitalized.
class Home extends CI_Controller{
}
?>
In object oriented programming, the classes are made up of functions(methods in other languages). One can be a class construct. This construct will run everytime the class is called, or instantiated. In PHP, the way to declare a contruct has changed. For those of you who are new to this, then you might not notice the change, but for those of use who have been around for a while, will see that, PHP used to call class construct functions with the same name as the class. With PHP 5+, we now have another way of doing this. We would call the construct like this.
Update: As mentioned in a comment below from Tobz, in order to reach the parent constructor, you need to specify it in the code. (This slipped from me). If you do not add this line of code, you will get errors.
PHP
<?php
// This file name is home.php
// The class name for this file should be the same
// filename, except it should be capitalized.
class Home extends CI_Controller{
function __construct(){
// Note that there are (2) underscores (_)
parent::__construct(); // Should always be the first thing you call.
}
}
?>
<?php
// This file name is home.php
// The class name for this file should be the same
// filename, except it should be capitalized.
class Home extends CI_Controller{
function __construct(){
// Note that there are (2) underscores (_)
parent::__construct(); // Should always be the first thing you call.
}
}
?>
As mentioned above, this construct will execute everytime this class is instantiated. Constructs are good to have, to give initial values to variables. Let’s create a variable date, and give the value of today’s date.
PHP
<?php
// This file name is home.php
// The class name for this file should be the same
// filename, except it should be capitalized.
class Home extends CI_Controller{
var $today;
function __construct(){
// Note that there are (2) underscores (_)
// To grab the class variable, (this) needs to be used
parent::__construct();
$this->today = date('d/m/Y');
}
}
?>
<?php
// This file name is home.php
// The class name for this file should be the same
// filename, except it should be capitalized.
class Home extends CI_Controller{
var $today;
function __construct(){
// Note that there are (2) underscores (_)
// To grab the class variable, (this) needs to be used
parent::__construct();
$this->today = date('d/m/Y');
}
}
?>
Before we start displaying anything on the browser, I would like to recommend an organized structure for your files. In many cases, we will be using a CSS file, or a javascript file for all our pages. In this case, we can make a header file, a footer file, and files with main content.
First create a folder named includes. This folder should reside inside of: application -> views -> includes. Within this folder, create 2 files. One called header.php, and the other footer.php. The header file should look something like this:
Filename: header.php
PHP
<!DOCTYPE HTML PUBliC '-//W3C//Dtd HTML 4.01 transitional//EN' 'http://www.w3c.org/tr/1999/REC-html401-19991224/loose.dtd'>
<html lang=en xmlns='http://www.w3.org/1999/xhtml'>
<head>
<title>Jotorres Web Development</title>
</head>
<body>
<!DOCTYPE HTML PUBliC '-//W3C//Dtd HTML 4.01 transitional//EN' 'http://www.w3c.org/tr/1999/REC-html401-19991224/loose.dtd'>
<html lang=en xmlns='http://www.w3.org/1999/xhtml'>
<head>
<title>Jotorres Web Development</title>
</head>
<body>
Notice it has an open body tag, but no finishing. This tag should be finished in the footer file.
Filename: footer.php
PHP
</body>
</html>
</body>
</html>
For now, we can leave this as is. Let’s go back to our controller, and let us show the page to the user. Within the controller, we would need to call the ‘view’ function to display the content of the main content file to the browser. Let’s create an index function, and send the variable of today to the view, and display todays’ date to the user. Also, we will call another file that is within the includes folder called template. This template file will load the header, the content, and then the footer, and display it in the browser. We will create an array named $data. This will be an associative array, with data that will tell the template file, which content to load, and also, send in the variable of todays date. Once inside the view, this array is converted to variables. Let’s see how.
PHP
<?php
// This file name is home.php
// The class name for this file should be the same
// filename, except it should be capitalized.
class Home extends CI_Controller{
var $today;
function __construct(){
// Note that there are (2) underscores (_)
// To grab the class variable, (this) needs to be used
parent::__construct();
$this->today = date('d/m/Y');
}
function index(){
// Create an array with the information that we need to send.
// First is the main content to display
$data['main_content'] = 'home_view';
// Next we send the date variable
$data['today'] = $this->today;
// Now that we have our information
// We will load the template.
// And send the array
$this->load->view('includes/template', $data);
}
}
?>
<?php
// This file name is home.php
// The class name for this file should be the same
// filename, except it should be capitalized.
class Home extends CI_Controller{
var $today;
function __construct(){
// Note that there are (2) underscores (_)
// To grab the class variable, (this) needs to be used
parent::__construct();
$this->today = date('d/m/Y');
}
function index(){
// Create an array with the information that we need to send.
// First is the main content to display
$data['main_content'] = 'home_view';
// Next we send the date variable
$data['today'] = $this->today;
// Now that we have our information
// We will load the template.
// And send the array
$this->load->view('includes/template', $data);
}
}
?>
Inside the includes folder, we need to add a new file named template.php
Filename: template.php
PHP
<?php
// Here we load the header
// the content, and the footer
$this->load->view('includes/header');
// We need to load the content file
$this->load->view($main_content); // This is the name we sent in $data['main_content']
// Now we load the footer
$this->load->view('includes/footer');
?>
<?php
// Here we load the header
// the content, and the footer
$this->load->view('includes/header');
// We need to load the content file
$this->load->view($main_content); // This is the name we sent in $data['main_content']
// Now we load the footer
$this->load->view('includes/footer');
?>
Notice, how we load each view file without the extension of .php. Codeigniter will automatically look for a file with that name and give it the proper extension. (They should have .php extension though).
Now, let’s create our content file, which should go in our views folder. Within this file, we can access the variable $today, since it was sent through our $data array. This file can be as simple as the following example.
Filename: home_view.php
PHP
<div>
<?php
echo "Today's date is: ";
echo $today;
?>
</div>
<div>
<?php
echo "Today's date is: ";
echo $today;
?>
</div>
And there you have it (Codeigniter for the absolute beginner). This is technically the most basic codeigniter can get for anyone trying to step into this framework world. Hope you all enjoy, and remember, feedbacks are always welcome.