Home  >  Article  >  Backend Development  >  PHP creates its own MVC framework_PHP tutorial

PHP creates its own MVC framework_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:19:35879browse

1. File structure
Create 3 folders
controller folder stores controller files
view folder stores view files
model folder stores data files
Create 1 index.php as the only entrance
2. Controller
We create a democontroller.php file under the controller folder. The content of the file is as follows

Copy 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 code The code is as follows:

require( 'controller/democontroller.php');
$controller=new 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 be run
$c_name=$c_str.'Controller';
//According to the agreement, the controller name obtained from the URL does not contain Controller. Complete 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 the controller file
$controller=new $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 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.
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 everyone can basically think of it by now. How to add 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:
Copy 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 under the view folder is as follows:
Copy code The code is as follows:



demo


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


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325267.htmlTechArticle1. Create 3 folders in the file structure. The controller folder stores the controller files. The view folder stores the view files and model files. Create an index.php folder to store data files as the only entrance...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn