PHP micro-framework design

Source: Internet
Author: User
I. before implementing a framework, we need to understand the effects of the framework. according to the traditional framework ideas, we can summarize the following: 1. implement the MVC architecture and separate the control, logic, and view layers. 2. encapsulate various functions and functional modules... I. Overall Framework Analysis
Before implementing a framework, we need to understand the effects of the framework. according to the traditional framework idea, we can summarize the following:
1. implement the MVC architecture and separate the control, logic, and view layers.
2. encapsulate various functions and functional modules to implement one write and multiple calls to reduce code redundancy.
3. easy to expand. you can easily introduce external extension libraries to enhance your own frameworks.
4. select the design mode and encapsulate or compile various engine modules.
The basic framework requirement is probably like this. with these requirements, the next step is an architecture design. many problems need to be solved here. next we will conduct a step-by-step analysis.

II. framework design process
1. framework Directory
This is actually a very important step. your selection is different, and the last directory structure will be very different. in addition to meeting the basic three-tier requirements, the extension Library, front-end files, templates, resource files also need to be placed in directories, which determines whether your call is convenient. in this framework, I used the smaty engine as the View engine. the directory structure is roughly as follows:

The url in single-entry mode is usually similar to index. php? Controller = controller & method = method. after obtaining the controller and method name through the get method, we can perform dynamic initialization 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) native method transformation
Anyone familiar with smarty should know that smarty has two methods: assign and dispaly, which are used to register variables and output variables to template files respectively, however, if multiple variables are registered at the same time, our code will become redundant, so we try to transform these 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);}

Let's rewrite The assign method so that it can directly register the array, which reduces the amount of subsequent code. if we want to introduce other external libraries, you can also use this method to transform native functions to make them more suitable.
(3) file inclusion logic
The startup file of this framework is pc. php, including pc. php basically contains the files needed by the entire framework. First, let's look at the index of the next entry file. php content.

header("Content-type:text/html;charset=utf-8");date_default_timezone_set('Asia/Shanghai');require_once('config.php');require_once('framework/pc.php');PC::run($config)

The configuration file and framework startup engine pc. php are included, and you can call the run method to start the framework. then you can check the content of pc. php.

$ Currentdir = dirname (_ FILE _); include_once ($ currentdir. '/include. list. php '); foreach ($ paths as $ path) {include_once ($ currentdir. '/'. $ path);}/*** to complete a series of initialization and call controllers */class PC {public static $ controller; public static $ method; private static $ config; private static function init_db () {DB: init ('mysql', self: $ config ['dbconfig']);} private static function init_view () {VIEW :: init ('smarty ', self: $ config ['viewconfi G']);} 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 );}}

Foreach traverses all files in include. list. php and passes the obtained controller and corresponding method to class C for automatic inclusion. Let's take a look at include. list. php.

$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');

An array is stored, including our two factory classes, database operation classes, external engine classes, and core function classes.
Now, you can sort out the process of processing a url request by the framework:

The url in single-entry mode is usually similar to index. php? Controller = controller & method = method. after obtaining the controller and method name through the get method, we can perform dynamic initialization 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) native method transformation
Anyone familiar with smarty should know that smarty has two methods: assign and dispaly, which are used to register variables and output variables to template files respectively, however, if multiple variables are registered at the same time, our code will become redundant, so we try to transform these 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);}

Let's rewrite The assign method so that it can directly register the array, which reduces the amount of subsequent code. if we want to introduce other external libraries, you can also use this method to transform native functions to make them more suitable.
(3) file inclusion logic
The startup file of this framework is pc. php, including pc. php basically contains the files needed by the entire framework. First, let's look at the index of the next entry file. php content.

header("Content-type:text/html;charset=utf-8");date_default_timezone_set('Asia/Shanghai');require_once('config.php');require_once('framework/pc.php');PC::run($config)

The configuration file and framework startup engine pc. php are included, and you can call the run method to start the framework. then you can check the content of pc. php.

$ Currentdir = dirname (_ FILE _); include_once ($ currentdir. '/include. list. php '); foreach ($ paths as $ path) {include_once ($ currentdir. '/'. $ path);}/*** to complete a series of initialization and call controllers */class PC {public static $ controller; public static $ method; private static $ config; private static function init_db () {DB: init ('mysql', self: $ config ['dbconfig']);} private static function init_view () {VIEW :: init ('smarty ', self: $ config ['viewconfi G']);} 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 );}}

Foreach traverses all files in include. list. php and passes the obtained controller and corresponding method to class C for automatic inclusion. Let's take a look at include. list. php.

$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');

An array is stored, including our two factory classes, database operation classes, external engine classes, and core function classes.
Now, you can sort out the process of processing a url request by the framework:

The basic design of the framework is like this. it is very simple, but basically implemented the mvc architecture. although it is a lot different from the mature frameworks on the market, the architecture of mvc will be further understood once rewritten, in addition, more and more websites use this single-entry mvc architecture, which requires a better understanding of the penetration of such websites.

For more articles about PHP micro-framework design, refer to PHP Chinese network!

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.