Summary of knowledge points in PHP Custom Framework section (i)
There are a number of good MVC frameworks available on the web, but the custom framework is not designed to develop a comprehensive, ultimate MVC framework solution, but rather as a good opportunity to learn PHP from the inside, and in the process of learning object-oriented programming and MVC design patterns, and learn some of the considerations in development.
The process of WEB MVC manifests itself as:
A typical web MVC process:
- The controller intercepts the user's request;
- The controller calls the corresponding model;
- Model calls DAO (the wrapper class based on the PDO operations database) to complete the read and write operation of the State;
- The controller transmits the data to the view;
- View renders the final result and presents it to the user.
First, create the appropriate catalog file.
Assume that the project name is a file in the Xframe,xframe directory once:
App App Catalog
---Home store foreground file
---- controller to store the front desk
---- model for storing the foreground
View files for displaying data ----View
---admin to store background files
---- Controller to store the background
---- model for background
View files for displaying data ----View
core files are stored
config to store the configuration file
public to store common files
Vendor Storing extension files
index.php Portal File
In order to set up the framework, you can accurately find the file in the corresponding directory, it is very necessary to setup the directory at the beginning.
1, set up the directory, the first thing is to create a good portal file (index.php) Set the following code:
1<?PHP2 3 //Add an entry constant to determine if you have permission to go directly to another file4 Define(' ACCESS ',TRUE);5 6 //Load Initialization class7 include' Core/app.class.php ';8 9 //go to initialization classTen //core\app::run (); One\core\app::run ();//because namespaces are used, you must take space when accessing classes
2-1, from the entry file code can be seen, the next need to create a file is in the Core directory of the App.class.php file.
1<?PHP2 3 //using Namespaces4 namespace Core;5 6 //permission to judge: Future files will be used in the portal file defined by the constant ' ACCESS ' to determine7 if(!defined(' ACCESS ')) {8 //Illegal access rights9 Header(' location:/index.php ');Ten die; One } A - classapp{ - //This method is the method that is called by the Portal file to initialize the the Public Static functionrun () { - Echo' Success '; -}
2-2, when the browser can see the output of the corresponding text, prove that the method can be successfully called, and then in the App class to define other initialization methods, used to set the character set, define directory constants, set system error control, interpret the URL, distribution controller .
Set the character set:
// Set character sets Private Static function Setcharset () { header("Content-type:text/html;charset=utf-8"); }
Define Directory constants:
//Defining Directory Constants Private Static functionSetdirconst () {//To set the root directory constant Root_dir Define(' Root_dir ',Str_replace(' Core ', ',Str_replace(‘\\‘,‘/‘,( __dir__))); //define the directory constants, the recommendations are added '/' Define(' Core_dir ', Root_dir. ' Core/')); Define(' Config_dir ', Root_dir. ' config/')); Define(' Vendor_dir ', Root_dir. ' vendor/')); Define(' App_dir ', Root_dir. ' app/')); Define(' Public_dir ', Root_dir. ' public/')); }
To set error control:
// setting error control Private Static function Setsyserror () { // Two levels: level display, display @ini_sete_all ); @ini_set(' display_errors ', 1); }
Explain the URL ($_request can get all the data passed to it, so when it is not possible to determine whether it is post or get, use $_request):
//Interpreting URLs Private Static functionIniurl () {//get three data: platform, controller, method $plat=isset($_request[' P ']) ?Trim($_request[' P ']) : ' Home '; $module=isset($_request[' C ']) ?Ucfirst(Strtolower($_request[' C '])) : ' Index '; $action=isset($_request[' A ']) ?$_request[' A ']: ' Index '; //this is used to store the platform, Controller, and method that the URL gets to by defining constants, because constants are available for global use. Define(' PLAT ',$plat);//Admin or Home Define(' CONTROLLER ',$module);//specify the controller or index Define(' ACTION ',$action);//Specify a method or index}
Distribute the controller and invoke the method:
//Distribution Controller Private Static functionInidispatch () {//get the corresponding platform, controller and method $module= CONTROLLER. ' Controller '; $action=ACTION; //instantiate the corresponding controller, and then call the method include_onceApp_dir. PLAT. ' /controller/'. CONTROLLER. ' Controller.class.php '; //currently the core space, you need to go to the corresponding controller space, space naming rules: Platform + corresponding attributes. New \home\controller\indexcontroller (); $module= ' \ \ '. PLAT. ' \\Controller\\ '.$module;//to construct a fully qualified name access path $module=New $module; //Var_dump ($module); $module-$action();//Variable Method}
Finally, the method defined above is called at the beginning of the entry method run () to enable App.class.php to initialize the appropriate settings when the portal file is called:
Public Static function run () { self:: Setcharset (); Sets the character set self::setdirconst (); Define Directory constants self::setsyserror (); Set error control self::iniurl (); Parse URL self::inidispatch (); Distributing controllers and calling methods }
When the above steps are complete, you can create the IndexController.class.php in the Controller in the Home directory under the APP directory
Summary of knowledge points in PHP Custom Framework section (i)