Summary of the shop mvc framework in the php Project (1), mvc framework _ PHP Tutorial

Source: Internet
Author: User
Summary of the shop mvc framework in the php Project (1). mvc framework. Summary of the shop mvc framework in the php Project (1), mvc framework 1. code structure division: current directory structure: site Root Directory application Directory Model Directory php store mvc framework summary (1), mvc framework

1Division of the code structure:

Current Directory structure: /site root directory/application directory Model/Model Directory View/View directory Back/Back-end front/test platform Controller/Controller Directory Back/Back-end front/front-end test/test platform/framework directory MySQLDB. class. php database operation class DAO Model. class. php basic model class/index. php entry file

2. request homepage

2.1 request the homepage parameter instance (request localhost/index. php? P = front & c = shop & a = index)

P = front // The back-end or front-end parameters include back and front C = index // controller. here, request the homepage controller A = shop // action. here, the homepage shop action is used.

2.2 Unified request code on the homepage

 

2.3 Framework Code

/*** Initialize basic functions of the Framework class */class Framework {/*** run entry of the project Framework class */public static function run () {self :: _ initPathConst (); // Initialization path constant self: _ initConfig (); // load the configuration self ::_ initDispatchParam (); // initialize the distribution parameter self :: _ initPlatformPathConst (); // initialize the path constant self: _ initAutoload (); // register the automatic loading method self: _ dispatch (); // request distribution }}

2.3.1 initialize path constant

/*** Initialize PATH constant */private static function _ initPathConst () {// determine the PATH constant define ('root _ path', getCWD () used in the project (). '/'); // The Project's root directory define ('app _ path', ROOT_PATH. 'application/'); // define ('con _ path', APP_PATH. 'controller/'); // controller directory define ('mod _ path', APP_PATH. 'model/'); // model Directory define ('Vie _ path', APP_PATH. 'view/'); // view layer directory define ('cfg _ path', APP_PATH. 'config/'); // the configuration file directory define ('frw _ path', ROOT_PATH. 'framework/'); // framework directory define ('tol _ path', FRW_PATH. 'tool/'); // tool directory define ('pub _ path', ROOT_PATH. 'Public/'); // define ('upd _ path', PUB_PATH. 'upload _ image/'); // upload the image directory}

2.3.2 load the configuration file

Private static function _ initConfig () {// load and load the configuration file, and save the value of the configuration item in $ config and global variables. $ GLOBALS ['config'] = require pai_path. 'application. config. php ';}

2.3.3 initialize distribution parameters

/*** Determine parameter p, c, a, distribution parameter, (route parameter) */private static function _ initDispatchParam () {// Obtain the platform parameter $ GLOBALS ['P'] = $ p = isset ($ _ GET ['P'])? $ _ GET ['P']: $ GLOBALS ['config'] ['app'] ['default _ platform ']; // p, platform // GET the controller class parameter $ GLOBALS ['c'] = isset ($ _ GET ['c'])? $ _ GET ['c']: $ GLOBALS ['config'] [$ p] ['default _ controller']; // c, controller // Obtain the action parameter $ GLOBALS ['A'] = isset ($ _ GET ['A'])? $ _ GET ['A']: $ GLOBALS ['config'] [$ p] ['default _ action']; // a, action}

The above code uses the initial loading configuration file to initialize the default request. when you directly request: localhost/index. php without parameters, load the system default parameters.

2.3.4 initialize path constants related to the platform

/*** Initialize the path constant related to the current platform * This is used to determine P and find out under which control */private static function _ initPlatformPathConst () {// The path constant define ('cur _ CON_PATH ', CON_PATH. $ GLOBALS ['P']. '/'); // The current platform's controller directory define ('cur _ VIE_PATH ', VIE_PATH. $ GLOBALS ['P']. '/'); // view layer Directory of the current platform}

2.3.4 register the automatic loading method

Private static function _ initAutoload () {// register to automatically load spl_autoload_register (array (_ CLASS __, 'selfautoload '));} the 'selfautoload' method is as follows: public static function selfAutoload ($ class_name) {// first checks whether it is the core class of the framework, class $ class_file = array ('model' => FRW_PATH. 'model. class. php', 'mysqldb' => FRW_PATH. 'mysqldb. class. php ', 'controller' => FRW_PATH. 'controller. class. php', 'sessiondb' => TOL_PATH. 'sessiondb. class. php', 'captcha '=> TOL_PATH. 'captcha. class. php', 'upload' => TOL_PATH. 'upload. class. php', 'image' => TOL_PATH. 'image. class. php', 'page' => TOL_PATH. 'page. class. php ',); if (isset ($ class_file [$ class_name]) {// is the core class require $ class_file [$ class_name];} // whether it is a Model class elseif (substr ($ class_name,-5) = 'model') {// Model class require MOD_PATH. $ class_name. '. class. php ';} // whether the Controller class is elseif (substr ($ class_name,-10) = 'controller') {// Controller class require CUR_CON_PATH. $ class_name. '. class. php ';}}

2.3.4 request distribution

/***** Request distribution * submit the request to a certain action of a controller to complete */private static function _ dispatch () {// instantiate the controller class, call the corresponding action method // ucfirst () function to convert the first character in the string to uppercase. $ Controller_name = ucfirst ($ GLOBALS ['c']). 'controller'; // match Match. controller // load controller class $ Controller = new $ controller_name; // variable class name // call action method $ action_name = $ GLOBALS ['A']. 'action'; $ controller-> $ action_name (); // variable method}

2.3.5 when we request localhost/index. php, it is equivalent to requesting localhost/index. php? P = front & c = shop & a = index.

The ShopController under application \ controller \ front. the request action is indexAction.

The indexAction code is as follows:

Public function indexAction () {// Obtain the classified data $ model_cat = new CatModel; $ cat_list = $ model_cat-> getNestedList (); // load the foreground homepage template require CUR_VIE_PATH. 'index.html ';}

Note:

1. ShopController inherits from the platform controller PlatformController. The Platform controller inherits from the basic controller class: controller

The link is as follows:

2. after determining the Control action in MVC, the next step is to implement the Model

$ Model_cat = new CatModel; -- "instantiate the catModel class $ cat_list = $ model_cat-> getNestedList (); --" get all foreground categories.

3. encapsulate all basic operation database methods in the basic model. the getNestedLIst method is as follows:

/*** Obtain the nested category list data */public function getNestedList ($ p_id = 0) {// Obtain all Categories $ list = $ this-> getList (); // create nested data and Recursively search for return $ this-> getNested ($ list, $ p_id );}

4. the getList method is as follows:

/*** Get list data */public function getList () {$ SQL = "select * from 'php _ category '"; return $ this-> _ db-> fetchAll ($ SQL );}

5. after the Model is implemented, the View is loaded.

// Load the foreground homepage template require CUR_VIE_PATH. 'index.html ';

2.3.6 conclusion: to implement a function, first determine the Control, then implement the Model, and finally load the View

2.3.7 do not elaborate on the front-end page

Partition (1), mvc framework 1. code structure division: current directory structure:/site root directory/application directory Model/Model Directory...

Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.