The CI framework implements the separation of the front and back ends of the framework.
In this article, we analyze the CI framework to implement the frontend and backend separation of the framework. We will share this with you for your reference. The details are as follows:
We are always used to separating the front-end and back-end of the development project when using the CI framework development (or using all other frameworks, place the controllers, models, or views in different folders. At this point, the ThinkPHP framework is more convenient than the CI framework. This blog post describes how to separate the front and back ends Based on the CI framework. (This article takes the Pkadmin background management system developed by myself as an example. The Pkadmin background management system is developed based on the Codeigniter_v3.1.0 framework and Olive Admin background template. I will not mention it here, if you are interested, you can go to Github to check whether you are interested in learning from each other.
Github address: https://github.com/zhihua-wei/Pkadmin)
1. Establish a reasonable directory structure
I believe everyone knows the directory structure of the CI framework and the functions of various directories, I will not repeat it here (if you still have something you don't quite understand, you can go to the blog column of the blogger ). To implement functional separation between the front-end and back-end frameworks, we must first establish a reasonable directory structure, that is, where you want to put your front-end controller, background controller, front-end view, and background management view.
For the directory structure of the Pkadmin background management system, I put all the controllers in the controllers folder of the Framework, and added only different front-end backend folders to store their own Controller files. The view is the same. Do not go into details.
2. Set the view path for the extended loader.
① For better project maintainability, I set the view path as a constant and define constants in App/config/constants. php:
// Define the foreground view path constant define ('home _ VIEW_DIR ', 'Views/HOME/'); // define the background view path constant define ('admin _ VIEW_DIR ', 'Views/Pkadmin/'); // define ('pkadmin _ version', '1. 0.0 ');
② Extended Loader
If you do not know how to expand the loader, you can view the CI framework manual. (For more information, create the PK_Loader.php file in the APP/core folder)
<? Php/*** ============================================ ======= * Created by Pocket Knife Technology. * Author: zhi1__w <zhihua_wei@foxmail.com> * Date: 2016/11/02 0031 * Time: * Project: Pkadmin background management system * Version: 1.0.0 * Power: loader extension * ============================================ ======= */defined ('basepath ') OR exit ('no direct script access allowed'); class PK_Loader extends CI_Loader {/*** sets the foreground view path */public function set_home_view_dir () {$ this-> _ ci_view_paths = array (APPPATH. HOME_VIEW_DIR => TRUE);}/*** sets the background view path */public function set_admin_view_dir () {$ this-> _ ci_view_paths = array (APPPATH. ADMIN_VIEW_DIR => TRUE );}}
3. Extended Controller
The extended controller is mainly used to generate the parent controller in the foreground and background for convenience and operation inheritance (the code is simplified to make it easy for you to quickly understand its principles ).
<? Php/*** ============================================ ======= * Created by Pocket Knife Technology. * Author: zhi1__w <zhihua_wei@foxmail.com> * Date: 2016/11/02 0031 * Time: * Project: Pkadmin background management system * Version: 1.0.0 * Power: controller extension * =================================================== ===== */defined ('basepath ') OR exit ('no direct script access allowed');/*** foreground parent controller */class Home_Controller extends CI_Controller {public function _ construct () {parent :: __construct (); $ this-> load-> set_home_view_dir () ;}}/*** background parent class controller */class Pkadmin_Controller extends CI_Controller {public $ data; public function _ construct () {parent ::__ construct (); $ this-> load-> set_admin_view_dir ();}}
4. Test
In the Pkadmin controller folder, create a new Admin. php (background homepage) controller file to test the output (the code is simplified ).
<? Php/*** ============================================ ======= * Created by Pocket Knife Technology. * Author: zhi1__w <zhihua_wei@foxmail.com> * Date: 2016/11/05 0015 * Time: am * Project: Pkadmin background management system * Version: 1.0.0 * Power: home Page controller * ==================================================== ======= */defined ('basepath ') OR exit ('no direct script access allowed'); class Admin extends Pkadmin_Controller {public function _ construct () {parent ::__ construct (); $ this-> load-> model ('login _ model', 'admin'); $ this-> load-> library ('pagination ');} /*** Pkadmin background homepage action */public function index () {$ this-> load-> view('admin.html ', $ data );}}
The background controller inherits the background parent class controller, and the foreground controller integrates the foreground parent class controller. Similarly, you can create a view file in the respective view folders to perfectly separate the front and back ends.