What is the MVC principle in PHP? Introduction to the MVC implementation Principle of PHP (code attached)

Source: Internet
Author: User
Tags php class php foreach
Part of the MVC in PHP is very well understood, for example, M is model models, complete the specific business logic, V is the View view, is responsible for displaying information, C is controller controllers, responsible for the command, the method to distinguish, in the foreground when the demand, Determine what method to use for processing, so what is the implementation of MVC in PHP? We specifically use code to understand the MVC principle in depth.

I. Structure of the document
Create 3 folders
Controller folder holds director files
View folder holds views file
Model folder holds data files
Establish 1 index.php as the sole entrance
Second, the controller
We set up a democontroller.php file under the Controller folder, the file content is as follows

<?php class Democontroller {function index () {echo (' Hello World ')}}/* End of File democontroller.php */

In this file we just set up an object named Democontroller and contains a method of index, which outputs Hello world. The index method in Democontroller is executed in index.php below.
The code for index.php is as follows

<?php require (' controller/democontroller.php '); $controller =new Democontroller (); $controller->index (); /* End of File index.php */

Run Index.php,ok wish we saw our long-lost Hello world. These two files are very simple, but also reveal a little bit of the nature of MVC, running the controller we want to run through the unique portal. Of course the controller part should be determined by the URI, so let's rewrite the index.php so that he can use the URI to decide to run the controller.
The index.php rewrite code is as follows:

<?php $c _str=$_get[' C ']; Gets the controller to run $c _name= $c _str. ' Controller '; The controller name obtained by the agreed URL does not contain a controller, which is padded here. $c _path= ' controller/'. $c _name. PHP '; According to the Convention controller file to be established under the Controller folder, the class name is the same as the file name, and the file name is all lowercase. $method =$_get[' a ']; Gets the action require to run ($c _path); Load controller file $controller =new $c _name; Instantiate controller file $controller-$method (); Run action/* End of File index.php */In this instance

Enter Http://localhost/index.php?c=demo&a=index in the browser and get our Hello world. Of course if we have another controller and want to run it, just modify the values of C and a in the URL parameters.
Here are a few questions to explain.
PHP is a dynamic language, we can directly through the string new out of the object we want and run the method we want, namely the above new $c _name, we can understand into new ' Democontroller ', because $c_ The value of name itself is ' Democontroller ', and of course the direct new ' democontroller ' is not possible, where the ' Democontroller ' string must be brokered by a variable. method is the same.
Second, we in the URL of the value of C is demo, that is, $c_name value should be Democontroller Ah, PHP is not case-sensitive, so also can run? PHP Case-sensitive sentence is not complete, in PHP only variables (preceded by $) and constants (define defined) are case-sensitive, and the class name, Dharma name even some keywords are case-insensitive. True,false,null and so on can only be capitalized or all lowercase. Of course we'd better be case-sensitive in the actual coding process.
Third, view
We just output a "Hello World" in the controller above, and do not achieve the effect of MVC, below I will add the view on this basis, I believe here you can basically think of how to add the View function. Yes, it is done through the require of evil or by the include.
First we create a index.php in the View folder, write something (hehe, I write Hello World). Then we rewrite our previous democontroller. The code is as follows:

<?php class Democontroller {function index () {require (' view/index.php ')}}/* End of File democontroller.php */

Run it in the browser again to see if we've already exported what we want.
Then we pass some data to the view through the controller, and the code looks like this:

<?php class Democontroller {function index () {$data [' title ']= ' first title '; $data [' List ']=array (' A ', ' B ', ' C ', ' D '); Require (' view/index.php '); }}/* End of File democontroller.php */

The index.php file code under the View folder is as follows:

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.