The first article has already set up the required catalogue, and the next job is to write some code.
Open the public/index.php file with the editor and write the following code
<?php define (DS, directory_separator); Define (ROOT, DirName (dirname (__file__))); $url = $_get[' url ']; Load boot require_once (ROOT. Ds. ' Core '. Ds. ' bootstrap.php ');
The code for the portal file is very concise, where it is a global variable that is used to obtain the parameters as requested
Now let's comment out the sentence that loads the boot, and then echo $url below, i.e.
Require_once (ROOT. Ds. ' Core '. Ds. ' bootstrap.php '); echo $url;
Then enter http://localhost/wudicsmvc/in the browser address bar
Note: D:\APPSERV\WWW\WUDICSMVC
Then, you will find that nothing is shown.
then enter Http://localhost/wudicsmvc/wudics
You'll find the page wudics the words
Note that the URL parameters can be obtained normally, this as a global variable exists, mainly used to obtain the client's request
Which method to access which controller
Http://localhost/wudicsmvc/home/index
You can specify that access to the home controller's index method, of course, you can also have different rules, that is, routing rules, is said to be a very important concept of MVC
The index.php file has no content, loads a bootstrap.php file, opens the bootstrap.php file under the core directory:
<?php //Load Configuration require_once (ROOT. Ds. ' CFG '. Ds. ' config.php '); Route request require_once (ROOT. Ds. ' Core '. Ds. ' route.php ');
Also loaded two files, one is config.php under the CFG directory, one is the core directory of the route.php
Look at the config.php.
<?php //mysql connection parameter define (' db_host ', ' localhost '); Define (' Db_user ', ' root '); Define (' Db_pass ', ' 123456 '); Define (' db_name ', ' transdb '); Some constants of Smarty define (' Dir_tpl ', ROOT. Ds. ' App '. Ds. ' View '. Ds. ' Template_dir '. Ds. ' Default '. DS); Define (' Dir_cpl ', ROOT. Ds. ' App '. Ds. ' View '. Ds. ' Compile_dir '. DS); Define (' Dir_cfg ', ROOT. Ds. ' App '. Ds. ' View '. Ds. ' Config_dir '. DS); Define (' DIR_CAC ', ROOT. Ds. ' App '. Ds. ' View '. Ds. ' Cache_dir '. DS); The default control class method $default [' controller '] = ' home '; $default [' action '] = ' index '; PULIBC folder path, convenient template design define (' WEBSITE ', ' http://localhost '); Define (' Webimg ', WEBSITE dirname (dirname ($_server[' php_self ')). '/public/img/');
some constants and global variables, which is useful because constants and global variables are shared resources in a single-entry PHP program
And then the route.php file.
<?php function Callhook () {global $url; Global $default; Get the $controller $action $param $param = Array (); $URLARR = Explode ("/", RTrim ($url, "/")); $controller = Array_shift ($URLARR); $action = Array_shift ($URLARR); $param = $URLARR; if ($controller = = "") {$controller = $default [' Controller ']; $action = $default [' Action ']; } if ($action = = "") {$action = $default [' Action ']; }//Control class writing rules Homecontroller->index $controllerName = Ucfirst ($controller). ' Controller '; $dispatch = new $controllerName ($controller, $action); if (Method_exists ($dispatch, Ucfirst ($action))) {Call_user_func_array (Array ($dispatch, Ucfirst ($action) ), $param); } else {/* Error Code for Method was not exists */die (' Method nOT exitsts.<br/> '); }}//Automatically load class function __autoload ($classname) {if (File_exists (ROOT. Ds. ' Core '. Ds. Strtolower ($classname). '. class.php ') {require_once (ROOT. Ds. ' Core '. Ds. Strtolower ($classname). '. class.php '); } else if (File_exists (ROOT. Ds. ' App '. Ds. ' Controller '. Ds. Strtolower ($classname). '. php ') {require_once (ROOT. Ds. ' App '. Ds. ' Controller '. Ds. Strtolower ($classname). '. php '); } else if (File_exists (ROOT. Ds. ' App '. Ds. ' Model '. Ds. Strtolower ($classname). '. php ') {require_once (ROOT. Ds. ' App '. Ds. ' Model '. Ds. Strtolower ($classname). '. php '); } else {/* Error Code for can not find the files * * die (' Class not found.<br/> '); }} callhook ();
This page, first defined two functions Callhook and __autoload, then the bottom of the page there is a sentence callhook (), so that the program began to execute.
Callhook function:
Its role is to locate the above mentioned controller, method, parameters
When the code executes here
The control class writing rule homecontroller->index $controllerName = Ucfirst ($controller). ' Controller '; $dispatch = new $controllerName ($controller, $action);
The program executes the __autoload method, which can automatically load the required files when you use the class
Do not need manual require, this can be convenient when various class library, convenient, do not need to manually load every time the class is called. PHP class files
The Method_exists method determines whether the method exists in this class, and if it exists, Call_user_func_array calls the method
Note: The __autoload method is some of the rules for loading class library files, which are defined according to your habits.
At this point, the program will jump to a certain controller of a certain method of execution. For example, the index method in the HomeController class, which is defined in your __autoload rule.
I'm here for the app/controller/homecontroller.php file.
<?phpclass HomeController extends controller{public function Index () { $user = new User (); $TPL = new TPL (); $tpl->set (' name ', $user->name); $tpl->render (' index.html ');} }
This method contains the user class (Model) and the TPL Class (view)
The user class is responsible for reading data from the database
The TPL class is responsible for displaying the data
So it can also be said that the Controller class has a connection, control the data and display, so that the two can be well bound together, model and view are separated
This allows you to implement the original idea of separating the PHP code from the HTML layout.
I expect that I can integrate a convenient use of PHP framework, I hope you come to QQ group to point, QQ group number: 667110936671109366711093
Write an MVC framework that belongs to PHP (ii)