The MVC tutorial exercises on IMOOC.
MVC is the basis of PHP and the boundary point of the advanced level it should be said that
First, prepare
工欲善其事 ~
Personal environment: windows10+wamp2.5+zendstudio12;
Project name: Mvceasyfirst;
File name: testController.class.php
FileName Controller class file type
Code standard: PSR-2;
Second, build
Controller
1<?PHP2 namespace Mvceasyfirst\controller\test;3 4 UseMvceasyfirst\model\test\testmodel;5 UseMvceasyfirst\view\test\testview;6 //The controller is the only place that needs to connect with the user7 //Controller "connecting"8 //Controller is responsible for: 1, receive instructions, 2, call the model and receive the model processing good data; 3. Call view (pass data to view)9 classTestControllerTen { One Public functionShow () A { - $testModel=NewTestmodel (); - $data=$testModel-get (); the $testView=NewTestView (); - $testView->display ($data); - } -}
Model
1<?PHP2 namespace Mvceasyfirst\model\test;3 4 //The model is responsible for: 1, receiving instructions, 2, processing and returning data5 //bidirectional, but only connected to the controller6 classTestmodel7 {8 Public functionget ()9 {Ten return"Hello world!"; One } A}
View
1<?PHP2 namespace Mvceasyfirst\view\test;3 4 //view is responsible for: 1, accept the instruction, 2, display the relevant results to the browser/user;5 //One-way access, C->v->user6 classTestView7 {8 Public functionDisplay$data)9 {Ten Echo $data; One } A}
Third, call
1<?PHP2 UseMvceasyfirst\controller\test\testcontroller;3 require_once' TestController.class.php ';4 require_once' TestModel.class.php ';5 require_once' TestView.class.php ';6 //MVC relies heavily on the introduction, recommended require, and I thought not quite the same7 //In fact, I personally think this problem is to be divided into the situation, see what needs, need to run (warning) or find the problem (error)8 9 $testController=NewTestController ();Ten $testController->show ();
Iv. Summary
Simple small example, intuitive understanding of the MVC Model!!!
Be careful to distinguish between the "responsibilities" of each module!
Then there is the situation of normative development:
V. Standardization of entry procedures and system catalogs
The application of MVC to production;
Single Entry mechanism : In a Web application, all requests point to a script file, such as alksdjfklajfklj.php, which means that all access to the program must pass through the portal.
Benefits: MVC can be implemented, and what is safe and what seems more convenient to control.
Normalization of the system catalog :
I was going to do it first, and then again.
Vi. simplifying m\v\c Calls
As I said earlier, MVC has a lot of references/calls, so for calls, it's better to write a function to integrate/encapsulate
1<?PHP2 namespace Mvc\func;3 //simplified functions used in some frameworks4 5 functionC$name,$method)6 {7 require_once'/libs/controller/'.$name.‘ Controller.class.php ';8 //eval (' $obj =new '. $name. ' Controller (), $obj-$method. ' (); ');9 $controller=$name.‘ Controller ';Ten $obj=New $controller(); One $obj-$method(); A } - - functionM$name) the { - require_once' libs/model/'.$name.‘ Model.class.php '; - $model=$name.‘ Model '; - $obj=New $model(); + return $obj; - } + A functionV$name) at { - require_once' libs/view/'.$name.‘ View.class.php '; - $view=$name.‘ View '; - $obj=New $view(); - return $obj; -}
Among them, the Eval function is the first time I use, the video is not safe, just replace the following.
1 <? PHP 2 require_once ' func.php '; 3 4 \mvc\func\c (' Test ', ' show ');
1 <? PHP 2 class TestController 3 {4 Public function Show () 5 {6 echo "Hello"; 7 }8 }
--------4/28 Update---------------
My environmental problems or something, dynamic instantiation class is not OK, temporarily give up, there is a master know why, please tell AH ~
Environment: zend12+php5.5
This part for the time being, the next part is Smarty
MVC Architecture Learning easyfirst--Come on, I love learning.