An in-depth analysis of PHP's MVC framework

Source: Internet
Author: User
Tags lowercase php file require

This article first describes the MVC implementation of PHP principles, our framework of the MVC part is based on this principle, but today's code is not a framework of code, just to explain the principle

I. Structure of documents

Create 3 folders

Controller Folder store controller file

View folder holds views file

The Model folder holds data files

Establish 1 index.php as the only entrance

Second, the controller

We set up a democontroller.php file under the Controller folder, which contains the following files

<?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 below in index.php.

Index.php's code 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 they also reveal a little bit of MVC's nature, running the controller we want to run through the only portal. Of course the controller part should be determined by the URI, so let's rewrite the index.php so that he can decide which controller to run through the URI.

index.php rewrite the code as follows:

<?php

$c _str=$_get[' C '];

Get the controller to run

$c _name= $c _str. ' Controller ';

The controller name obtained in accordance with the agreed URL does not contain controller, which is padded here.

$c _path= ' controller/'. $c _name. PHP ';

By convention controller files 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

$controller =new $c _name;

Instantiating controller files

$controller-> $method ();

To run the action under this 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 other controller and want to run it, just modify the value of C and a in the URL parameter.

Here are a few questions to explain.

One, PHP is a dynamic language, we can directly through the string new to the object we want to run the method we want, that is, the new $c _name, we can understand as new ' Democontroller ', because $c_ The value of name itself is ' Democontroller ', of course, the direct new ' Democontroller ' so written is not possible, where the ' Democontroller ' string must pass a variable to relay. method is the same.

Second, we in the URL of the value of C is a demo, that is, $c_name value should be Democontroller Ah, PHP is not case-sensitive, this can also run it? PHP case sensitivity is not complete, in PHP only variables (preceded with $) and constants ( Define defined) is case-sensitive, while class names, Dharma name, and even some keywords are case-insensitive. and True,false,null and so on can only all uppercase or all lowercase. Of course we'd better be case-sensitive in the actual coding process.

Third, view

We in the previous controller just output a "Hello World", and did not achieve the effect of MVC, below I will add the view function on this basis, I believe that we basically can think of how to add the View function. Yes, it is done through the evil require or include.

First we set up a index.php under the View folder, write something (hehe, I write Hello World). Then we'll rewrite our previous democontroller. The code is as follows:

<?php

Class Democontroller

{

Function index ()

{

Require (' view/index.php ');

}

}

/* End of File democontroller.php * *

Then run it in the browser to see if it has already exported what we want.

Then we pass some data to view through controller, the code is as follows:

<?php

Class Democontroller

{

Function index ()

{

$data [' title ']= ' a 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:

<title>demo</title>

<body>

<?php

foreach ($data [' list '] as $item)

{

echo $item. ' <br> ';

}

?>

</body>

Finally MVC is Model view Controller

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.