Bản đóng gói lại thư mục system framework của CodeIgniter, sử dụng tương thích với Composer và PHP 7, PHP 8
- Support mô hình HMVC
- Support class Base Model với 1 số hàm cơ bản
- Hỗ trợ Output Response trên giao diện CLI thông qua hàm
ResponseOutput::writeLn($message)
- Bổ sung class
StatusCodes
khai báo sẵn các HTTP code tuân chuẩn (from Symfony framework), VD:StatusCodes::HTTP_OK
. Chi tiết tham khảo thêm tại classStatusCodes
- Cài đặt gói vào trong dự án với lệnh sau
composer require nguyenanhung/codeigniter-framework
- Cập nhật file
index.php
Tìm dòng
/*
*---------------------------------------------------------------
* SYSTEM DIRECTORY NAME
*---------------------------------------------------------------
*
* This variable must contain the name of your "system" directory.
* Set the path if it is not in the same directory as this file.
*/
$system_path = 'system';
Sửa thành như sau
/*
*---------------------------------------------------------------
* SYSTEM DIRECTORY NAME
*---------------------------------------------------------------
*
* This variable must contain the name of your "system" directory.
* Set the path if it is not in the same directory as this file.
*/
$system_path = '/your_vendor_path/nguyenanhung/codeigniter-framework/system';
- Xoá thư mục
system
trong thư mục gốc dự án đi cho gọn
Trong thư viện đã xây dựng sẵn 1 Base Controller, kế thừa như sau
- Xây dựng 1
Controller
mới theo tài liệu CodeIgniter 3 - Kế thừa class từ
HungNG_CI_Base_Controllers
thay vìCI_Controller
, ví dụ như sau
<?php
/**
* Class Hungna_test
*
* @author 713uk13m <dev@nguyenanhung.com>
* @copyright 713uk13m <dev@nguyenanhung.com>
*/
class Hungna_test extends HungNG_CI_Base_Controllers
{
public function __construct()
{
parent::__construct();
}
public function index()
{
echo "This is ".get_class($this); // show: This is Hungna_test
exit();
}
}
- Xây dựng 1 model theo tài liệu CodeIgniter 3
- Kế thừa class từ
HungNG_Custom_Based_model
thay vìCI_Model
, ví dụ như sau
<?php
defined('BASEPATH') or exit('No direct script access allowed');
/**
* Class Credentials_model
*
* @author 713uk13m <dev@nguyenanhung.com>
* @copyright 713uk13m <dev@nguyenanhung.com>
* @property \CI_DB_query_builder $db
*/
class Credentials_model extends HungNG_Custom_Based_model
{
const IS_ACTIVE = 1;
const ROLE_PUSH = 1;
const ROLE_PULL = 2;
const ROLE_FULL = 3;
protected $fieldUsername;
protected $fieldStatus;
protected $fieldRole;
/**
* Credentials_model constructor.
*
* @author : 713uk13m <dev@nguyenanhung.com>
* @copyright: 713uk13m <dev@nguyenanhung.com>
*/
public function __construct()
{
parent::__construct();
$this->db = $this->load->database('default', true, true);
$this->tableName = 'credentials';
$this->primary_key = 'id';
$this->fieldUsername = 'username';
$this->fieldStatus = 'status';
$this->fieldRole = 'role';
}
}
- Create folder:
modules
trong thư mụcapplication
. Tham khảo cấu trúc thư mụcmodules
tạisample/modules/
trong thư viện này
.
└── modules
└── startup
├── config
│ ├── index.html
│ └── routes.php
├── controllers
│ ├── Startup.php
│ └── index.html
├── index.html
├── models
│ ├── Startup_model.php
│ └── index.html
└── views
└── index.html
6 directories, 8 files
- Create file
hmvc.php
với nội dung như sau
<?php
defined('BASEPATH') or exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| HMVC settings
| -------------------------------------------------------------------------
| See: https://github.com/nguyenanhung/CodeIgniter-HMVC
|
*/
$config['modules_locations'] = array(
APPPATH . 'modules/' => '../modules/'
);
- Nạp file
hmvc.php
vào fileconfig.php
require_once __DIR__ . '/hmvc.php';
- Create file
MY_Loader.php
trong thư mụcapplication/core/
có nội dung như sau
<?php
defined('BASEPATH') or exit('No direct script access allowed');
/**
* Class MY_Loader
*
* @author 713uk13m <dev@nguyenanhung.com>
* @copyright 713uk13m <dev@nguyenanhung.com>
*/
class MY_Loader extends HungNG_Loader
{
}
- Create file
MY_Router.php
trong thư mụcapplication/core/
có nội dung như sau
<?php
defined('BASEPATH') or exit('No direct script access allowed');
/**
* Class MY_Router
*
* @author 713uk13m <dev@nguyenanhung.com>
* @copyright 713uk13m <dev@nguyenanhung.com>
*/
class MY_Router extends HungNG_Router
{
}
- Triển khai viết code trong thư mục modules mới, tương tự như sau
<?php
defined('BASEPATH') or exit('No direct script access allowed');
/**
* Class TestModule
*
* @author 713uk13m <dev@nguyenanhung.com>
* @copyright 713uk13m <dev@nguyenanhung.com>
*/
class TestModule extends HungNG_CI_Base_Module
{
public function __construct()
{
parent::__construct();
}
public function index()
{
echo "This is ".get_class($this); // show: This is TestModule
exit();
}
}
Name | Skype | ||
---|---|---|---|
Hung Nguyen | dev@nguyenanhung.com | nguyenanhung5891 | @nguyenanhung |