MVC framework PHP builds its own MVC framework

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

Copy the Code code as follows:


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

Copy the Code code as follows:


Require (' controller/democontroller.php ');
$c 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:

Copy the Code code as follows:


$c _str=$_get[' C '];
Get 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 '];
Get the action to run
Require ($c _path);
Load Controller file
$c $c _name;
Instantiating a controller file
$controller $method ();
Run the action under the instance
/* End of File index.php */


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:

Copy the Code code as follows:


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:

Copy the Code code as follows:


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:

Copy the Code code as follows:




Demo



foreach ($data [' list '] as $item)
{
echo $item. '
';
}
?>


The above describes the MVC framework PHP to build their own MVC framework, including the content of the MVC framework, I hope to be interested in PHP tutorial friends helpful.

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