This article first introduces the MVC implementation principle of PHP. The MVC part of our framework is also implemented based on this principle, but today's code is not The code within the framework is only for illustrating the principle
1. File structure
Create 3 folders
The controller folder stores controller files
The view folder stores view files
The model folder stores data files
Create 1 index.php as the only entrance
2. Controller
We create a democontroller.php file in the controller folder. The content of the file is as follows
class DemoController
{
Function index()
{
echo('hello world');
}
}
/* End of file democontroller.php */
In this file we just created an object named DemoController and contains an index method, which outputs hello world. Next, execute the index method in DemoController in index.php.
The code of index.php is as follows
require('controller/democontroller.php');
$controller=new DemoController();
$controller->index();
/* End of file index.php */
Run index.php, ok as expected we can see our long lost hello world. These two files are very simple, but they also reveal a little bit of the essence of MVC, running the controller we want to run through the only entrance. Of course, the controller part should be determined by the uri, so let's rewrite index.php so that it can determine which controller to run through the uri.
Rewrite index.php code as follows:
$c_str=$_GET['c'];
//Get the controller to run
$c_name=$c_str.'Controller';
//According to the agreement, the controller name obtained from the URL does not contain Controller, so fill it in here.
$c_path='controller/'.$c_name.'.php';
//According to the agreement, the controller file must be created in the controller folder, the class name must be the same as the file name, and the file name must be all lowercase.
$method=$_GET['a'];
//Get the action to run
require($c_path);
//Load controller file
$controller=new $c_name;
// Instantiate controller file
$controller->$method();
//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 another controller and want to run it, we only need to modify the values of c and a in the url parameters.
There are several issues to explain here.
1. PHP is a dynamic language. We can directly get the object we want and run the method we want through the string new, that is, the new $c_name above, we can understand it as new 'DemoController', because $c_name The value itself is 'DemoController'. Of course, writing new 'DemoController' directly will not work. The 'DemoController' string must be transferred through a variable. The method is the same.
Second, the value of c in our URL is demo, which means that the value of $c_name should be demoController. Isn’t PHP case-sensitive? Can this also run? The sentence PHP is case-sensitive is incomplete. In PHP, only variables (preceded by $) and constants (defined by define) are case-sensitive, while class names, method names and even some keywords are not case-sensitive. And true, false, null, etc. can only be all uppercase or all lowercase. Of course we'd better be case-sensitive during the actual encoding process.
3. View
We only output a "hello world" in the previous controller, which did not achieve the effect of mvc. Next, I will add the view function on this basis. I believe that by now everyone can basically think of how to add the view function. Yes, it is achieved through the evil require or include.
First, we create an index.php under the view folder and write anything (haha, I still wrote hello world). Then we rewrite our previous DemoController. The code is as follows:
class DemoController
{
Function index()
{
require('view/index.php');
}
}
/* End of file democontroller.php */
Run it in the browser again to see if the content we want has been output.
Then we pass some data to the view through the controller. The code is 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 in the view folder is as follows:
foreach ($data['list'] as $item)
{
echo $item.'
';
}
?>
In the end, MVC is Model View Controller Model View Controller