: This article mainly introduces the shop mvc framework summary in the php Project (1). If you are interested in the PHP Tutorial, please refer to it. 1. code structure division:
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
The above describes the summary of the mvc framework in the php project shop (1), including the content, and hope to be helpful to friends who are interested in the PHP Tutorial.