HMVC Lessons by Aalex HMVC For The Absolute Beginner.: Latest Version
HMVC Lessons by Aalex HMVC For The Absolute Beginner.: Latest Version
HMVC Lessons by Aalex HMVC For The Absolute Beginner.: Latest Version
Next, after you have downloaded the files, just extract the content and place them into your root
folder. Thats 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'] =
?>
1 <?php
2 $config['base_url'] = 'http://localhost/ci_basics';
3 ?>
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 its 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
1 <?php
2 $route['default_controller'] = 'home';
3 ?>
Thats enough configuration for today. Now lets create our home controller. Controllers are just
PHP classes that are within the folder controller. Each class should have the same name as its
file. Also, each class will extend from codeigniters main controller. Lets see how.
PHP
<?php
// This file name is home.php
// The class name for this file sh
// filename, except it should be
1 <?php
2 // This file name is home.php
3 // The class name for this file should be the same
4 // filename, except it should be capitalized.
5
6 class Home extends CI_Controller{
7
8}
9 ?>
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 sh
// filename, except it should be
1
<?php
2
// This file name is home.php
3
// The class name for this file should be the same
4
// filename, except it should be capitalized.
5
6
class Home extends CI_Controller{
7
function __construct(){
8
// Note that there are (2) underscores (_)
9
parent::__construct(); // Should always be the first thing you call.
10
}
11
}
12
?>
13
As mentioned above, this construct will execute everytime this class is instantiated. Constructs
are good to have, to give initial values to variables. Lets create a variable date, and give the
value of todays date.
PHP
<?php
// This file name is home.php
// The class name for this file sh
// filename, except it should be
1 <?php
2 // This file name is home.php
3 // The class name for this file should be the same
4 // filename, except it should be capitalized.
5
6 class Home extends CI_Controller{
7 var $today;
8 function __construct(){
9 // Note that there are (2) underscores (_)
10 // To grab the class variable, (this) needs to be used
11 parent::__construct();
12 $this->today = date('d/m/Y');
13 }
14 }
15 ?>
16
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
'http://w w w .w 3c.org/tr/1999/RE
<html lang=en xmlns='http://w w
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>
1 </body>
2 </html>
For now, we can leave this as is. Lets 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. Lets 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.
Lets see how.
PHP
<?php
// This file name is home.php
// The class name for this file sh
// filename, except it should be
1
2 <?php
3 // This file name is home.php
4 // The class name for this file should be the same
5 // filename, except it should be capitalized.
6
7 class Home extends CI_Controller{
8 var $today;
9 function __construct(){
10 // Note that there are (2) underscores (_)
11 // To grab the class variable, (this) needs to be used
12 parent::__construct();
13 $this->today = date('d/m/Y');
14 }
15 function index(){
16 // Create an array with the information that we need to send.
17 // First is the main content to display
18 $data['main_content'] = 'home_view';
19 // Next we send the date variable
20 $data['today'] = $this->today;
21 // Now that we have our information
22 // We will load the template.
23 // And send the array
24 $this->load->view('includes/template', $data);
25 }
26 }
27 ?>
28
29
Inside the includes folder, we need to add a new file named template.php
Filename: template.php
PHP
<?php
// Here w e load the he
// the content, and the
$this->load->view ('inc
1 <?php
2 // Here we load the header
3 // the content, and the footer
4 $this->load->view('includes/header');
5 // We need to load the content file
6 $this->load->view($main_content); // This is the name we sent in $data['main_content']
7 // Now we load the footer
8 $this->load->view('includes/footer');
9 ?>
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, lets 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 "Toda
echo $today
1 <div>
2 <?php
3 echo "Today's date is: ";
4 echo $today;
5 ?>
6 </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.