Write an MVC framework that belongs to PHP (ii)

Source: Internet
Author: User
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


   
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:

    
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.

     
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.

    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.
'); }}//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.
'); }} 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.

 
     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

  • 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.