PHP Mini-Frame Design

Source: Internet
Author: User
First, the framework of the overall analysis
Before implementing a framework, we need to understand how the framework should achieve a result, according to the traditional framework of the idea, can roughly summarize the following:
1. Implement the MVC architecture, separating the control, logic, and view layers.
2. Encapsulate various functions and function modules, achieve one writing, multiple calls, reduce code redundancy.
3. Easy to expand, easy to introduce external expansion library, to enhance their own framework.
4. Choose a design pattern, package or write various engine modules.
Basic framework requirements are probably this way, with these requirements, the next is an architectural design, which involves a lot of problems need to be solved, below we step by step to do an analysis.

Second, the framework design process
1. Framework Catalogue
This is actually a very important step, your choice is different, the final directory structure will also have a very big difference, in addition to meet the basic three layer, the extension library, front-end files, templates, resource files and so also need to find directory placement, and this determines after your call is convenient, My framework design uses the Smaty engine as the view engine, the directory structure is probably as follows:

This diagram shows a structure of the level two directory, the more in-depth catalogs due to the limited image display is not all out, and each directory and the role of the main file will be mentioned in the following.

2. Directory Description
(1) The Data directory
can be seen in the directory structure, due to the use of the Smarty engine, and in the Smarty engine need to configure the buffer directory and cache directory, so the data directory is the role of this.
(2) Framework directory
This directory is at the heart of our framework, and the DB directory has our database operations library. The function directory places a function.php file that is primarily useful for quickly instantiating calls to various layers of methods in a pattern such as M (' do '). The Libs directory places the Core factory class files of the framework, such as database operations classes, view manipulation classes, and facilitates the invocation of the methods in the external file in such a way as db::funtion (). View places the Smarty views engine. Include.list.php is the file that our framework needs to include when it is called, and it is placed in an array for saving. Pc.php is our framework startup engine, responsible for unifying the initialization of our various modules and parsing our URLs.
(3) img Directory
for placing our style files, JS files, and other related resource files
(4) Libs directory
The directory and the Libs directory under the framework have the same name, but you can see that This directory is placed in the MVC three layer of the corresponding business processing content and an org extension directory, controller placement controllers, model placement models, view placement views processing class.
(5) TPL directory
This directory will put some template files for the front-end display, you can see I placed the admin and index two directories, respectively, for the foreground and background template file storage.
(6) admin.php and index.php
in general, the use of the MVC architecture uses the single-entry mode, and these two files are single-entry mode entry files to start the framework.
(7) config.php
This basic framework has a configuration file that includes the configuration of the database, the configuration of the Smarty engine, and the definition of some static variables.
All of these structures are just one basic structure of the mini-framework, and in fact complex frameworks have many extended functions and external plugins that can be adapted to this directory structure.

3. Framework key points
(1) Dynamic Call of controller
The common URL of a single-entry mode is probably similar to the Index.php?controller= controller &method= method, which can be dynamically initialized by getting the controller and method names through the Get method, as follows.

function C ($name, $method) {require_once ('/libs/controller/'. $name. ' Controller.class.php '); eval (' $obj =new '. $ Name. ' Controller (); $obj. $method. ' ();');} function M ($name) {require_once ('/libs/model/'. $name. ' Model.class.php '); eval (' $obj =new '. $name. ' Model (); '); return $obj;} function V ($name) {require_once ('/libs/view/'. $name. ' View.class.php '); eval (' $obj =new '. $name. ' View (); '); return $obj;}

(2) Transformation of native methods
Familiar with smarty friends should know, Smarty have assign and dispaly two methods, respectively, for registering variables and output variables to the template file, but if you register multiple variables at the same time will make our code very jumbled, so we try to transform the two methods

public static function assign ($data) {foreach ($data as $key = = $value) {self:: $view->assign ($key, $value);}} public static function display ($template) {self:: $view->display ($template);}

We let the Assign method rewrite, so that it can register the array directly, which reduces our subsequent code, if we want to introduce other external libraries, you can also use this method to transform the native function to make it more applicable.
(3) file contains logic
The boot file for this framework is pc.php, so the inclusion of pc.php basically contains the files needed for the entire framework, first look at the contents of a portal file index.php.

Header ("Content-type:text/html;charset=utf-8");d ate_default_timezone_set (' Asia/shanghai '); Require_once (' Config.php '); require_once (' framework/pc.php '); Pc::run ($config)

It's easy to include the configuration file and framework startup engine pc.php, and then call the Run method to start the framework and then look at the contents of pc.php

$currentdir =dirname (__file__); include_once ($currentdir. ' /include.list.php '), foreach ($paths as $path) {include_once ($currentdir. ') /'. $path);} /*** completes a series of initialization and call controllers */class pc{public static $controller;p ublic static $method;p rivate static $config;p rivate static function init_db () {db::init (' MySQL ', self:: $config [' dbconfig ']);} private static function Init_view () {view::init (' Smarty ', Self:: $config [' viewconfig ']);} private static function Init_controller () {self:: $controller =isset ($_get[' controller '))? Daddslashes ($_get[' Controller ']): ' Index ';} private static function Init_method () {self:: $method =isset ($_get[' method '))? Daddslashes ($_get[' method '): ' Index ';} public static function run ($config) {self:: $config = $config; self::init_db (); Self::init_view (); Self::init_controller (); Self::init_method (); C (self:: $controller, Self:: $method);}}

The foreach traversal contains all the files in the include.list.php, and the controller and corresponding method gets passed to the class C for automatic inclusion. And see what include.list.php has.

$paths = $arrayName = Array (' function/function.php ', ' libs/core/db.class.php ', ' libs/core/view.class.php ', ' db/ Mysql.class.php ', ' view/smarty/smarty.class.php ');

This stores an array of our two factory classes, database operations classes, external engine classes, and core function classes.
At this point, you can comb the entire framework for a URL request processing process:

(4) Separation of business
The core of MVC is the strict separation between the layers, but the controller layer and model are often easily confused, which will lead to the MVC architecture loses its original meaning, we need to understand that the control layer only implements simple control and logic processing, does not involve the specific business and data interaction, All specific operations should be placed on the model layer. Also, the class names and filenames in the two tiers should be consistent.
(5) Method control
When we call the controller and the method via the URL, some methods do not want to be called externally, such as login check function, we can prevent the risk by defining the function as a private function to avoid it being called directly through the URL.
(6) Extensibility Design
A framework should have good extensibility, especially for new external libraries, which should be easy to use with simple modifications, so the configuration items should be separated separately from storage.

Iii. Summary
The basic design of the framework is this, very simple, but the basic implementation of the MVC architecture, although there are many differences with the mature framework on the market, but rewrite the structure of the MVC understanding will be more thorough, and today more and more sites are using this single-entry MVC architecture, the penetration of such sites need a good understanding.


First, the framework of the overall analysis
Before implementing a framework, we need to understand how the framework should achieve a result, according to the traditional framework of the idea, can roughly summarize the following:
1. Implement the MVC architecture, separating the control, logic, and view layers.
2. Encapsulate various functions and function modules, achieve one writing, multiple calls, reduce code redundancy.
3. Easy to expand, easy to introduce external expansion library, to enhance their own framework.
4. Choose a design pattern, package or write various engine modules.
Basic framework requirements are probably this way, with these requirements, the next is an architectural design, which involves a lot of problems need to be solved, below we step by step to do an analysis.

Second, the framework design process
1. Framework Catalogue
This is actually a very important step, your choice is different, the final directory structure will also have a very big difference, in addition to meet the basic three layer, the extension library, front-end files, templates, resource files and so also need to find directory placement, and this determines after your call is convenient, My framework design uses the Smaty engine as the view engine, the directory structure is probably as follows:

This diagram shows a structure of the level two directory, the more in-depth catalogs due to the limited image display is not all out, and each directory and the role of the main file will be mentioned in the following.

2. Directory Description
(1) The Data directory
can be seen in the directory structure, due to the use of the Smarty engine, and in the Smarty engine need to configure the buffer directory and cache directory, so the data directory is the role of this.
(2) Framework directory
This directory is at the heart of our framework, and the DB directory has our database operations library. The function directory places a function.php file that is primarily useful for quickly instantiating calls to various layers of methods in a pattern such as M (' do '). The Libs directory places the Core factory class files of the framework, such as database operations classes, view manipulation classes, and facilitates the invocation of the methods in the external file in such a way as db::funtion (). View places the Smarty views engine. Include.list.php is the file that our framework needs to include when it is called, and it is placed in an array for saving. Pc.php is our framework startup engine, responsible for unifying the initialization of our various modules and parsing our URLs.
(3) img Directory
for placing our style files, JS files, and other related resource files
(4) Libs directory
The directory and the Libs directory under the framework have the same name, but you can see that This directory is placed in the MVC three layer of the corresponding business processing content and an org extension directory, controller placement controllers, model placement models, view placement views processing class.
(5) TPL directory
This directory will put some template files for the front-end display, you can see I placed the admin and index two directories, respectively, for the foreground and background template file storage.
(6) admin.php and index.php
in general, the use of the MVC architecture uses the single-entry mode, and these two files are single-entry mode entry files to start the framework.
(7) config.php
This basic framework has a configuration file that includes the configuration of the database, the configuration of the Smarty engine, and the definition of some static variables.
All of these structures are just one basic structure of the mini-framework, and in fact complex frameworks have many extended functions and external plugins that can be adapted to this directory structure.

3. Framework key points
(1) Dynamic Call of controller
The common URL of a single-entry mode is probably similar to the Index.php?controller= controller &method= method, which can be dynamically initialized by getting the controller and method names through the Get method, as follows.

function C ($name, $method) {require_once ('/libs/controller/'. $name. ' Controller.class.php '); eval (' $obj =new '. $ Name. ' Controller (); $obj. $method. ' ();');} function M ($name) {require_once ('/libs/model/'. $name. ' Model.class.php '); eval (' $obj =new '. $name. ' Model (); '); return $obj;} function V ($name) {require_once ('/libs/view/'. $name. ' View.class.php '); eval (' $obj =new '. $name. ' View (); '); return $obj;}

(2) Transformation of native methods
Familiar with smarty friends should know, Smarty have assign and dispaly two methods, respectively, for registering variables and output variables to the template file, but if you register multiple variables at the same time will make our code very jumbled, so we try to transform the two methods

public static function assign ($data) {foreach ($data as $key = = $value) {self:: $view->assign ($key, $value);}} public static function display ($template) {self:: $view->display ($template);}

We let the Assign method rewrite, so that it can register the array directly, which reduces our subsequent code, if we want to introduce other external libraries, you can also use this method to transform the native function to make it more applicable.
(3) file contains logic
The boot file for this framework is pc.php, so the inclusion of pc.php basically contains the files needed for the entire framework, first look at the contents of a portal file index.php.

Header ("Content-type:text/html;charset=utf-8");d ate_default_timezone_set (' Asia/shanghai '); Require_once (' Config.php '); require_once (' framework/pc.php '); Pc::run ($config)

It's easy to include the configuration file and framework startup engine pc.php, and then call the Run method to start the framework and then look at the contents of pc.php

$currentdir =dirname (__file__); include_once ($currentdir. ' /include.list.php '), foreach ($paths as $path) {include_once ($currentdir. ') /'. $path);} /*** completes a series of initialization and call controllers */class pc{public static $controller;p ublic static $method;p rivate static $config;p rivate static function init_db () {db::init (' MySQL ', self:: $config [' dbconfig ']);} private static function Init_view () {view::init (' Smarty ', Self:: $config [' viewconfig ']);} private static function Init_controller () {self:: $controller =isset ($_get[' controller '))? Daddslashes ($_get[' Controller ']): ' Index ';} private static function Init_method () {self:: $method =isset ($_get[' method '))? Daddslashes ($_get[' method '): ' Index ';} public static function run ($config) {self:: $config = $config; self::init_db (); Self::init_view (); Self::init_controller (); Self::init_method (); C (self:: $controller, Self:: $method);}}

The foreach traversal contains all the files in the include.list.php, and the controller and corresponding method gets passed to the class C for automatic inclusion. And see what include.list.php has.

$paths = $arrayName = Array (' function/function.php ', ' libs/core/db.class.php ', ' libs/core/view.class.php ', ' db/ Mysql.class.php ', ' view/smarty/smarty.class.php ');

This stores an array of our two factory classes, database operations classes, external engine classes, and core function classes.
At this point, you can comb the entire framework for a URL request processing process:

(4) Separation of business
The core of MVC is the strict separation between the layers, but the controller layer and model are often easily confused, which will lead to the MVC architecture loses its original meaning, we need to understand that the control layer only implements simple control and logic processing, does not involve the specific business and data interaction, All specific operations should be placed on the model layer. Also, the class names and filenames in the two tiers should be consistent.
(5) Method control
When we call the controller and the method via the URL, some methods do not want to be called externally, such as login check function, we can prevent the risk by defining the function as a private function to avoid it being called directly through the URL.
(6) Extensibility Design
A framework should have good extensibility, especially for new external libraries, which should be easy to use with simple modifications, so the configuration items should be separated separately from storage.

Iii. Summary
The basic design of the framework is this, very simple, but the basic implementation of the MVC architecture, although there are many differences with the mature framework on the market, but rewrite the structure of the MVC understanding will be more thorough, and today more and more sites are using this single-entry MVC architecture, the penetration of such sites need a good understanding.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.