The MVC development model is not here to repeat
There are many mature MVC frameworks, and the purpose of this article is to help beginners understand how MVC works
Straight-in topics
Where application directory: Includes configuration file, controller directory, model, view
Myphp Framework Directory: Core files, database operations classes, helper classes, third party classes, etc.
Core directory: Controller base class, Framework core class, model base class
After creating these basic directories, create the index.php directory at the root of the project, which is the portal for all requests
<? PHP // Introducing Framework Core classes include "myphp/core/framework.class.php"; // Call initialization method Framework::run ();
In Framework.class.php, the request address is parsed, the corresponding controller is instantiated, and the corresponding method is executed.
<?PHPclassframework{ Public Static functionrun () {//the order of execution here cannot be wrongSelf::init (); Self::auto_load (); Self::route (); } //define controller, view, configuration file path, get current request controller and method name Public Static functioninit () {Define("DS",directory_separator); Define("ROOT",GETCWD().DS); Define("App_path", ROOT.) Application ".DS); Define("Framework_path", ROOT.) "Myphp".DS); Define("Public_path", ROOT.) "Public".DS); Define("Model_path", App_path. "Model".DS); Define("View_path", App_path. "View".DS); Define("Controller_path", App_path. "Controller".DS); Define("Config_path", App_path. "Config".DS); Define("Core_path", Framework_path. "Core".DS); Define("Db_path", Framework_path. "Database".DS); Define("Helper_path", Framework_path. "Helpers".DS); Define("Lib_path", Framework_path. "Libraries".DS); //Obtain the current platform controller method from the user request Define("PLATFORM",isset($_request[' P '])?$_request[' P ']: "Home"); Define("CONTROLLER",isset($_request[' C '])?$_request[' C ']: "Index"); Define("ACTION",isset($_request[' a '])?$_request[' A ']: "Index"); //get the path to the current request controller and view Define("Cur_controller_path", Controller_path. PLATFORM.DS); Define("Cur_view_path", View_path. PLATFORM.DS); //get File Upload path Define("Upload_path", Public_path. " Upload ".DS); //manually loading core classes requireCore_path. " Controller.class.php "; requireCore_path. " Model.class.php "; requireDb_path. " Mysql.class.php "; //Loading configuration Files $GLOBALS[' config '] =includeConfig_path. " Config.php "; //usually put in the public file header, so that the page calls will not conflict, set time zone, encoding, open OB and other operations generally put//method head of the benefits can be automatically executed when the program is called, and does not repeat the execution Session_Start(); } //Routing Methods Public Static functionRoute () {//determine the class name and method name $controller _name= CONTROLLER. " Controller "; $action _name= ACTION. " Action "; //instantiating the controller class $controller=New $controller _name; //Implementation Methods $controller-$action _name(); } //Register Auto-load//When new is a nonexistent class, the method registered in Spl_autoload_register is automatically called and the class famous as a parameter passed in Public Static functionauto_load () {Spl_autoload_register (Array(__class__, "Load")); } //Load Method Public Static functionLoad$className){ //controller classes and model classes, such as Goodscontroller,adminmodel if(substr($className, -10) = = ' Controller '){ requireCur_controller_path. "{$className}.class.php "; } ElseIf(substr($className,-5) = = "Model"){ requireModel_path. "{$className}.class.php "; } Else { //other circumstances, no } } }
Test: Enter Http://localhost/Shop/index.php?p=Admin&c=Index&a=index in the address bar
Call the Indexaction method under the Indexcontroller controller for personal use
<? PHP class indexcontroller{ publicfunction indexaction () { echo " This is the index method under controller controllers "; } }
PHP in-depth interpretation of the MVC framework (i)