


How to use CI framework to separate the front and back ends of the framework
This article mainly introduces the method of CI framework to realize the separation of front-end and back-end of the framework. It analyzes in detail the relevant views, controller settings and expansion of the front-end and back-end separation of CI framework in the form of examples. Friends in need can refer to it. Next
The example of this article analyzes the method of CI framework to realize the separation of front-end and back-end of the framework. Share it with everyone for your reference, the details are as follows:
When we develop using the CI framework (or develop using all other frameworks), we are always used to separating the front-end and back-end of the developed project. Place respective controllers, models or views in different folders. At this point, the ThinkPHP framework is more convenient than the CI framework. In this blog post, we will describe how to separate the front-end and back-end implementation based on CI framework development. (This article takes the Pkadmin backend management system developed by me as an example. The Pkadmin backend management system is developed based on the Codeigniter_v3.1.0 framework and the Olive Admin backend template. I will not introduce too much here. Interested friends can check it out on Github. Welcome Everyone corrects and learns from each other.
1. Establish a reasonable directory structure
I believe everyone already knows the directory structure of the CI framework and the functions of each directory. Here I won’t introduce it repeatedly (if you still don’t understand it well, you can read the blogger’s blog column). To realize the functional separation of the front-end and back-end frameworks, we must first establish a reasonable directory structure, that is, if you want to separate your Where should the front-end controller be placed, where should the background controller be placed, in which folder should the front-end view be placed, and in which folder should the background management view be placed.
As shown above It is the directory structure of the Pkadmin background management system. I put all the controllers in the controllers folder of the framework itself. I only added different front-end and background folders to store their respective controller files. The same is true for the views. No further details.
2. The extension loader sets the view path
① In order to have better maintainability of the project, I set the view path to a constant in App/config/constants.php Define constants in:
//定义前台视图路径常量 define('HOME_VIEW_DIR', 'views/Home/'); //定义后台视图路径常量 define('ADMIN_VIEW_DIR', 'views/Pkadmin/'); //版本 define('PKADMIN_VERSION', '1.0.0');
② Extension loader
How to extend the loader. If there are bloggers who don’t know how to do it, you can check out the CI framework. The manual is very clear. (One more thing: create the PK_Loader.php file in the APP/core folder)
<?php /** * ========================================== * Created by Pocket Knife Technology. * Author: ZhiHua_W <zhihua_wei@foxmail.com> * Date: 2016/11/02 0031 * Time: 上午 9:19 * Project: Pkadmin后台管理系统 * Version: 1.0.0 * Power: 加载器扩展 * ========================================== */ defined('BASEPATH') OR exit('No direct script access allowed'); class PK_Loader extends CI_Loader { /** * 设置前台视图路径 */ public function set_home_view_dir() { $this -> _ci_view_paths = array(APPPATH . HOME_VIEW_DIR => TRUE); } /** * 设置后台视图路径 */ public function set_admin_view_dir() { $this -> _ci_view_paths = array(APPPATH . ADMIN_VIEW_DIR => TRUE); } }
3. Expansion controller
Extension controller is mainly for the convenience of generating parent controllers in the front and backend and for operation inheritance (for convenience, you can quickly understand the principle, the code has been streamlined).
<?php /** * ========================================== * Created by Pocket Knife Technology. * Author: ZhiHua_W <zhihua_wei@foxmail.com> * Date: 2016/11/02 0031 * Time: 上午 9:23 * Project: Pkadmin后台管理系统 * Version: 1.0.0 * Power: 控制器扩展 * ========================================== */ defined('BASEPATH') OR exit('No direct script access allowed'); /** * 前台父控制器 */ class Home_Controller extends CI_Controller { public function __construct() { parent::__construct(); $this -> load -> set_home_view_dir(); } } /** * 后台父类控制器 */ class Pkadmin_Controller extends CI_Controller { public $data; public function __construct() { parent::__construct(); $this -> load -> set_admin_view_dir(); } }
#4. Test
Create a new Admin.php (backend home page) controller file in the Pkadmin controller folder for output Test (the code has been deleted and streamlined).
<?php /** * ========================================== * Created by Pocket Knife Technology. * Author: ZhiHua_W <zhihua_wei@foxmail.com> * Date: 2016/11/05 0015 * Time: 上午 10:23 * Project: Pkadmin后台管理系统 * Version: 1.0.0 * Power: 后台主页控制器 * ========================================== */ 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 后台首页action */ public function index() { $this -> load -> view('admin.html', $data); } }
The background controller inherits the background parent class controller, and the front desk controller integrates the front desk parent class controller, the same Establishing the corresponding view files in the respective view folders is the perfect separation of the front and back ends.
The above is the entire content of this article. I hope it will be helpful to everyone's learning. For more related content, please pay attention to the PHP Chinese website !
Related recommendations:
About the implementation of infinite classification and recursion in the CI framework
About the CodeIgniter framework verification code class Analysis of library files and usage
How to solve 404 errors in Nginx and CI framework
The above is the detailed content of How to use CI framework to separate the front and back ends of the framework. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

With the development of network technology, PHP has become one of the important tools for Web development. One of the popular PHP frameworks - CodeIgniter (hereinafter referred to as CI) has also received more and more attention and use. Today, we will take a look at how to use the CI framework. 1. Install the CI framework First, we need to download the CI framework and install it. Download the latest version of the CI framework compressed package from CI's official website (https://codeigniter.com/). After the download is complete, unzip

How to use the module system in Java 9 to separate and isolate code As the scale of software continues to expand, the complexity of the code continues to increase. In order to better organize and manage code, Java9 introduced the module system. The emergence of the module system solves the problem of traditional package dependencies, making the separation and isolation of code easier and more flexible. This article will introduce how to use the module system in Java 9 to achieve code separation and isolation. 1. Definition of module In Java9, we can use the module keyword to define

PHP is a popular programming language that is widely used in web development. The CI (CodeIgniter) framework is one of the most popular frameworks in PHP. It provides a complete set of ready-made tools and function libraries, as well as some popular design patterns, allowing developers to develop Web applications more efficiently. This article will introduce the basic steps and methods of developing PHP applications using the CI framework. Understand the basic concepts and structures of the CI framework. Before using the CI framework, we need to understand some basic concepts and structures. Down

Title: Suggestions on front-end technology selection in Golang front-end and back-end separation development As the complexity and demands of web applications continue to increase, the front-end and back-end separation development model is becoming more and more popular. In this development model, the backend is responsible for processing business logic, and the frontend is responsible for displaying pages and interacting with users. The two communicate through APIs. For development teams using Golang as a back-end language, choosing the right front-end technology is crucial. This article will discuss the recommended front-end technologies to choose in the separate development of front-end and back-end in Golang, and

PHP is a widely used server-side scripting language, and CodeIgniter4 (CI4) is a popular PHP framework that provides a fast and excellent way to build web applications. In this article, we will get you started using the CI4 framework to develop outstanding web applications by walking you through how to use it. 1. Download and install CI4 First, you need to download it from the official website (https://codeigniter.com/downloa

With the development of the Internet and its continuous integration into people's lives, the development of network applications has become more and more important. As a well-known programming language, PHP has become one of the preferred languages for developing Internet applications. Developers can use numerous PHP frameworks to simplify the development process, one of the most popular is the CodeIgniter (CI) framework. CI is a powerful PHP web application framework. It has the characteristics of lightweight, easy to use, optimized performance, etc., allowing developers to quickly build

The steps to introduce CSS styles in the CI framework are as follows: 1. Prepare CSS files; 2. Store the CSS files in the appropriate location of the CI framework project; 3. In the pages that need to use CSS styles, introduce CSS through the HTML <link> tag File; 4. Use the CSS class or ID name in the HTML element to apply the corresponding style.

PHP developers often face database operations during the website development process. As a very popular database management system, MySQL's read-write separation is one of the important means to improve website performance. In PHP development, implementing MySQL read-write separation can greatly improve the website's concurrent access capabilities and user experience. This article will introduce in detail the implementation method of MySQL read-write separation, and provide specific PHP code examples to help PHP developers better understand and apply the read-write separation function. What is MySQL read
