MVC framework PHP creates its own MVC framework
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 are here Create a democontroller.php file under the controller folder. The file content is as follows
Copy the code The code 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
Copy the codeThe code is as follows:
require('controller/democontroller.php');
$c DemoController();
$controller-> index();
/* End of file index.php */
Run index.php, ok as expected we saw 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.
index.php rewrite the code as follows:
Copy the code The code is 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 be run
require($c_path);
//Load the controller file
$c $c_name;
//Instantiate the 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 , got 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 a few questions 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 itself The value 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.
2. The value of c in our URL is demo, which means the value of $c_name should be demoController. Isn’t PHP case-sensitive? Can it run like this? 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. written. 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 in the view folder and write anything (haha, I still wrote hello world). Then we rewrite our previous DemoController. The code is as follows:
Copy the code 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 let’s pass some data to the view through the controller. The code is as follows:
Copy the code 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:
Copy the code The code is as follows:
< head>
foreach ($data['list'] as $item)
{
echo $item.'
';
}
?>