A summary of the store MVC framework in the PHP project (1)

Source: Internet
Author: User
Tags php database
1. Division of Code Structure:

current directory structure:/ site root /application/ application Catalog    model/ model catalog    View/ View directory        back/ backend        front/         test    PlatformController/ Controller directory        / Backstage        front/ foreground        test/ test Platform /framework/ Framework Catalog    mysqldb. class . PHP database Operations Class DAO    Model. class . PHP base Model class /index.php portal file

2. Request Home

2.1 Request Home Parameter instance (Request Localhost/index.php?p=front&c=shop&a=index)

P=front  // backstage or foreground parameters have back and front c=index   // Controller, here request Home Controller a= Shop   // action, here homepage Shop Action

2.2 First Unified request code

 
  PHP// First Load frame class './framework/framework.class.php'; // Run the project Framework::run ();

2.3 Framework Class Code

/** */class  Framework {    /** *     Project Framework     class run entry  */     Public Static function Run () {        self::_initpathconst (); // Initializing Path Constants        Self::_initconfig (); // Load Configuration        Self::_initdispatchparam (); // Initializing distribution parameters        Self::_initplatformpathconst (); // initializing platform-dependent path constants        Self::_initautoload (); // registering the Auto-load method        Self::_dispatch (); // Request Distribution     }}

2.3.1 Initializing Path constants

/** * Initialize Path constants*/    Private Staticfunction _initpathconst () {//determine the path constants used in the projectDefine'Root_path', GETCWD ().'/');//the root directory of the projectDefine ('App_path', Root_path.'application/');//application DirectoryDefine'Con_path', App_path.'controller/');//Controller DirectoryDefine'Mod_path', App_path.'model/');//Model CatalogDefine'Vie_path', App_path.'view/');//View-level catalogsDefine'Cfg_path', App_path.'config/');//Configuration file DirectoryDefine ('Frw_path', Root_path.'framework/');//Framework CatalogDefine'Tol_path', Frw_path.'tool/');//Tools CatalogDefine ('Pub_path', Root_path.'public/');//Public Resources DirectoryDefine'Upd_path', Pub_path.'upload_image/');//Upload a picture catalog}

2.3.2 Loading configuration files

Private Static function _initconfig () {        // load the load profile and save the value of the configuration item with the $CONFIG, global variable.         $GLOBALS ['config'application.config.php ' ;    }

2.3.3 Initialization of distribution parameters

/** * Determine p,c,a parameters, distribution parameters, (routing parameters)*/    Private Staticfunction _initdispatchparam () {//Get platform Parameters$GLOBALS ['P'] = $p = Isset ($_get['P']) ? $_get['P']: $GLOBALS ['Config']['app']['Default_platform'];//P,platform//get controller class parameters$GLOBALS ['C'] = Isset ($_get['C']) ? $_get['C']: $GLOBALS ['Config'[$p] ['Default_controller'];//C,controller//Get Action Parameters$GLOBALS ['a'] = Isset ($_get['a']) ? $_get['a']: $GLOBALS ['Config'[$p] ['default_action'];//a,action}

The above code is used in the initial load configuration file, initialize the default request, when you directly request: localhost/index.php, no parameters, load the system default parameters

2.3.4 initialization of platform-dependent path constants

/* *     Initialize the current platform-dependent path Constants     * This is used to judge P, to find out exactly     which control */    privatestatic function _initplatformpathconst () {        // path constant associated with the current platform        define (' Cur_con_path ', Con_path. $GLOBALS ['P'/'); // Controller directory        for the current platform Define ('cur_vie_path', Vie_path. $GLOBALS ['P' ' / '); // View-level directory for the current platform    }

2.3.4 Registration Automatic Loading method

Private Staticfunction _initautoload () {//register for automatic loadingSpl_autoload_register (Array (__class__,'Selfautoload')); }'Selfautoload'methods are as follows Public Staticfunction Selfautoload ($class _name) {//The class that can be determined in the framework is first judged by whether it is a framework core 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 classrequire $class _file[$class _name]; }            //is a model classElseIf (substr ($class _name,-5) =='Model') {            //Model ClassRequire Mod_path. $class _name.'. class.php'; }        //is the controller classElseIf (substr ($class _name,-Ten) =='Controller') {            //Controller ClassRequire Cur_con_path. $class _name.'. class.php'; }    }

2.3.4 Request Distribution

/* *     * Request Distribution     * Send the request to one of the controller's     actions complete */     privatestatic  Function _ Dispatch () {        // Instantiate the controller class, and invoke the corresponding action method         //Ucfirst () The function converts the first character in a string to uppercase.         $controller _name = Ucfirst ($GLOBALS ['C'Controller '; // match match. Controller        // Loader class        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, the equivalent of a request Localhost/index.php?p=front&c=shop&a=index is initialized

Shopcontroller controller under Application\controller\front, request action is indexaction

The Indexaction code is as follows:

 Public function Indexaction () {        // get classified data        new  Catmodel;         = $model _cat->getnestedlist ();         // loading front page templates        ' index.html ' ;    }

It should be stated that:

1, Shopcontroller Inheritance and Platform controller Platformcontroller, platform controller inherits from the base controller class: Controller

The relationship is as follows:

2, in determining good MVC, control action, the next step is to implement the model

   New Catmodel;   --"Catmodel class     = $model _cat->getnestedlist ();  --"Get all front desk categories

3, in the basic model, encapsulates all the basic operational database methods, wherein the Getnestedlist method is as follows

/* *     * Get nested categorical list data      */public      function getnestedlist ($p _id=0 ) {        // get all classifications        $list = $this andgetList ();         // make nested data, recursively find        return $this-getnested ($list, $p _id);    }

4. The GetList method is as follows

/* *     * get list data      *       /Public Function getList ()        {"  SELECT * from ' php_category ';         return $this->_db->fetchall ($sql);    }

5. After the model is implemented, it is loaded into view

    // loading front page templates    ' index.html ';

2.3.6 Summary: Implement a function, first determine control, then implement model, and finally load the view

2.3.7 Front Page not elaborated

The above describes the PHP project in the store MVC framework Summary (1), including aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • Related Article

    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.