Buat MHCV di Codeigniter Versi 3.xx

Setup Konsep MHVC / (Hierarchical Model View Controller) di Framework CodeIgniter versi > 3, Beda dengan MVC Codeigniter Default, Dengan HMVC memungkinkan memisahkan folder projects menjadi per modules. Misalkan membuat modules User maka akan memiliki folder Model, View, helper dan Controller sendiri.

Contoh Dengan konsep susunan folder:

modules > user :

  • register > controllers, views, models,..
  • dashbard > controllers, views, models,..
  • live > controllers, views, models,..

Kelebihan HMVC adalah dapat mengerjan suatu project dengan bersama tim. Saat mengerjakan suatu modules, maka tidak akan menggangu modules yang lainnya.

Konsep MHVC CodeIgniter memudahkan membuat aplikasi berbasis website lebih besar dan komplek namun tersusun rapi sehingga jika ada error / update code modules lain tetap bisa berjalan.

 

SETUP HMVC di CodeIgniter

Langkah awal download file codeigniter versi 3 unzip dan rename menjadi “ci3”, selanjutnya edit folder application menjadi “app” setelah itu edit kode pada folder index.php di root folder aplikasi:

kode:

$application_folder = 'app';

Download module MHVC Codeigniter

Modul codeigniter versi 3 ini mendukung PHP versi > 7.2  , berikut file : MHVC_Modules.zip

Extrak file, copy pada folder app:

note: folder asli core & third_party hapus, ganti kan folder baru mhvc modul.

READ :  Pengertian, Jenis Web Hosting Domain + DNS Lengkap

 

Buat Folder modules

ini menjadi folder kerangka kerja tersusun, buat folder baru didalam folder ‘app’ > modules > user :

  • controllers
  • views
  • models, bisa juga helper

Isi code folder controllers dengan file: Coba.php 

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Coba extends CI_Controller {

public function index()
{

$this->load->view('user/v_coba');
}}

None: perbedaan cuma pada code load view menambahkan ‘user’ sebelum ‘v_’coba’.

 

Buat file views > v_coba.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>User - Login</title>

<style type="text/css">

::selection { background-color: #E13300; color: white; }
::-moz-selection { background-color: #E13300; color: white; }

body {
background-color: #fff;
margin: 40px;
font: 13px/20px normal Helvetica, Arial, sans-serif;
color: #4F5155;
}

a {
color: #003399;
background-color: transparent;
font-weight: normal;
}

h1 {
color: #444;
background-color: transparent;
border-bottom: 1px solid #D0D0D0;
font-size: 19px;
font-weight: normal;
margin: 0 0 14px 0;
padding: 14px 15px 10px 15px;
}

code {
font-family: Consolas, Monaco, Courier New, Courier, monospace;
font-size: 12px;
background-color: #f9f9f9;
border: 1px solid #D0D0D0;
color: #002166;
display: block;
margin: 14px 0 14px 0;
padding: 12px 10px 12px 10px;
}

#body {
margin: 0 15px 0 15px;
}

p.footer {
text-align: right;
font-size: 11px;
border-top: 1px solid #D0D0D0;
line-height: 32px;
padding: 0 10px 0 10px;
margin: 20px 0 0 0;
}

#container {
margin: 10px;
border: 1px solid #D0D0D0;
box-shadow: 0 0 8px #D0D0D0;
}
</style>
</head>
<body>

<div id="container">
<h1>User - Codeigniter 3 MHCV</h1>

<div id="body">
<p>The page you are looking at is being generated dynamically by CodeIgniter.</p>

<p>If you would like to edit this page you'll find it located at:</p>
<code>app/modules/user/views/v_coba.php</code>

<p>The corresponding controller for this page is found at:</p>
<code>app/modules/controllers/Coba.php</code>

<p>If you are exploring CodeIgniter for the very first time, you should start by reading the <a href="user_guide/">User Guide</a>.</p>
</div>

<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds. <?php echo (ENVIRONMENT === 'development') ? 'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?></p>
</div>

</body>
</html>

OK, kode selesai.

READ :  Kenapa Hasil Google Adsense Versi AMP Menurun ?

 

Setup Routes

Fungsi dari setup kode routes adalah menentukan file awal default yang akan di akses, pada folder: app > config > routes.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

$route['default_controller'] = 'user/coba';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

Note: menentukan controllers awal yang akan di akases adalah ‘user/coba’.

 

Ujicoba MHVC

Setelah semua setingan diatas selesai maka bisa coba akses: localhost/ci3/

Berhasil, membuat konsep MHVC pada framework codeigniter versi > 3.

Download file jadi: ci3_MHVC.Zip


Updated: February 12, 2024