HMVC part two with examples
For Experienced developers in codeigniter
Hello ,
Now that we have our HMVC enabled instance of CodeIgniter , if you don’t know how to do that please check the following link
First let’s examine some examples
We will made in this tutorial hmvc example login module
1- First create the “login” folder module in our “hmvc_ci/application” directory. It should end up looking like this in your web directory
- hmvc_ci/application/modules/login/models/
- hmvc_ci/application/modules/login/views/
- hmvc_ci/application/modules/login/controllers
2- Second Create the “site” folder module in our “hmvc_ci/application” directory. It should end up looking like this
- hmvc_ci/application/modules/site/models/
- hmvc_ci/application/modules/site/views/
- hmvc_ci/application/modules/site/controllers
3- Copy this files to your login module you
Note : can download source code for this code from here
- hmvc_ci/application/modules/login/models/membership_model.php
- hmvc_ci/application/modules/login/views/login_form.php
- hmvc_ci//application/modules/login/views/signup_form.php
- hmvc_ci//application/modules/login/views/signup_successful.php
- hmvc_ci//application/modules/login/controllers/login.php
4- Next copy this files into site module
hmvc_ci/application/modules/site/controllers/site.php
hmvc_ci/application/modules/site/views/logged_in_area.php
5- Open “hmvc_ci/application/config/autoload.php” and edit it to look like the this:
If you have not already done so from step one, open “hmvc_ci/application/config/config.php” and edit it so that the base url is set to your appropriate location.
$config[‘base_url’] = ‘http://localhost/hmvc_ci‘;
6- Open “hmvc_ci/application/config/database.php” and add the appropriate links to your database.
[php]
$db[‘default’][‘hostname’] = “localhost”; // location of DB serverdb[‘default’]
[‘username’] = “YOUR USERNAME HERE”; // username you use to connect
$db[‘default’][‘password’] = “YOUR PASSWORD HERE”; // associated password
$db[‘default’][‘database’] = “hmvc_ci”; // The database you want to use
[/php]
7- Create this table from sql in phpmyadmin
[php]
CREATE TABLE `hmvc_ci`.`membership` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`first_name` VARCHAR( 32 ) NOT NULL ,
`last_name` VARCHAR( 32 ) NOT NULL ,
`email_address` VARCHAR( 64 ) NOT NULL ,
`username` VARCHAR( 32 ) NOT NULL ,
`password` VARCHAR( 32 ) NOT NULL
) ENGINE = MYISAM ;
[/php]
8 – So we have our triads grouped, but we are still not quite in HMVC mode yet. In the site controller we find a function called is_logged_in() which check if user logged in or not .
[php]
function is_logged_in(){
$is_logged_in = $this->session->userdata(‘is_logged_in’);
if(!isset($is_logged_in) || $is_logged_in != true){
echo ‘You don’t have permission to access this page. <a href=”../login”>Login</a>’;
die();
}
}
[/php]
This is a login related function. In MVC mode, this is required because site cannot access login. With HMVC we can fix this.
In other way we collect related functions to other class and run what we need from it it’s good for big projects based on many views we handle that by hmvc and here we go i’ll demonstrate that in login function
- Cut the is_logged_in() function out of “applications/modules/site/controllers/site.php“
- Save site.php without the is_logged_in() function.
- Open “applications/modules/login/controllers/login.php“.
- Paste the is_logged_in() function into the class.
- Save login.php and code will be like that
9 – Open “applications/modules/site/controllers/site.php“.
Note : for CI 2.x like my examples login function should be like this
[php]
function is_logged_in($session){
$is_logged_in = $session->userdata(‘is_logged_in’);
if(!isset($is_logged_in) || $is_logged_in != true)
{
echo ‘You don’t have permission to access this page. <a href=”../login”>Login</a>’;
die();
//$this->load->view(‘login_form’);
}
}
[/php]
10- Site controller __construct should be like this
In chapter three we will demonstrate more example on HMVC for user profile member
after user login we will give every user control panel
Wait our demo !
Best regards 🙂