The concept of a Framework may have frightened a lot of PHP Coder at the beginning. In fact, you only need to understand a Framework. In fact, nothing happens, but the functions of others are more complicated and more details are considered, higher code quality. The most important file of the framework is the entry file, basically equivalent... "> <LINKhref =" http://www.php100.com//statics/styl
The concept of a Framework may have frightened a lot of PHP Coder at the beginning. In fact, you only need to understand a Framework. In fact, nothing happens, but the functions of others are more complicated and more details are considered, higher code quality.
The most important file of the framework is the entry file, which is basically equivalent to a general control switch. All requests must go through this file:
| 2 |
Define ('app _ path', dirname (_ FILE _). '/...'); // application PATH |
| 3 |
Include APP_PATH. '/Library/Toper/Core/FrontController. class. php'; // import the front-end controller |
| 4 |
$ FrontController = Tp_FrontController: getInstance (); |
| 5 |
$ FrontController-> run (); |
This file can only do one thing, that is, hand over control to the framework.
As we all know, for an MVC application, all requests must go through the controller. so let's write a simple controller:
| 2 |
Class Test_IndexController extends Tp_Controller { |
| 3 |
Public function indexAction (){ |
This class only needs to inherit from Tp_Controller. Tp_Controller is the base class of the Toper controller. as long as you inherit it, you can use the framework to help you write a series of methods, in this way, you can greatly reduce your workload, and according to the specifications, the function name is also meaningful, for example, the function just written indicates accessing the index Action under the controller IndexContrller of the Test module.
So how to use the model? It is actually very simple.
| 2 |
Class Test_IndexModel extends Tp_Model { |
| 3 |
Public function test (){ |
Like a controller, it also inherits the framework class and then customizes the method. there is no restriction on the method name in this place.
Then how can we call the model in the controller after writing the model? in fact, the method is to instantiate the model class in the controller and then call the corresponding method, so we can modify the class of the controller just now:
| 2 |
Class Test_IndexController extends Tp_Controller { |
| 3 |
Public function indexAction (){ |
| 4 |
$ Model = new Test_IndexModel (); |
| 5 |
Echo $ model-> test (); |
Why is there no include statement?
That's because the framework helps you complete all this. of course, the framework is not omnipotent. when you are familiar with it, you can choose not to use automatic import.
Everything we did just now has no View, so how to write the View is actually an HTML file. The framework generally implements the tag library at the View layer, the tag library helps you to do things that originally needed PHP code, such as loop traversal. without the tag library, you may only need to use it in the View file. This is not good for code separation.
If you have no concept about the tag library, you can directly look at the following code!
This is a piece of code with no difficulty. The only difficulty may be the print tag, which is defined by the framework. its function is very simple. it is to print a string, you may think this is meaningless, because I can easily use PHP code to complete this function, but why do you need to understand PHP, if the tag library is used, front-end developers can operate data just like HTML tags.
We know that all requests must pass through the controller, so the view file cannot be directly accessed from the outside, so we need to modify the controller code again !!!
| 2 |
Class Test_IndexController extends Tp_Controller { |
| 3 |
Public function indexAction (){ |
| 4 |
$ Model = new Test_IndexModel (); |
| 5 |
Echo $ model-> test (); |
| 6 |
$ This-> _ display ('test. test '); |
If you have learned smarty, you may be familiar with the display method. In fact, this function is used to display a template file !!!
Well, a basic MVC application is built up. it's not very difficult !!!
There are two ways to view the effect:
1. CGI:
Open your browser. if your domain name is localhost/testframework, you can use localhost/testframework/Public/index. php/Test/Index/index for access. if you have set a virtual host, such as www.a.com, you only need to use www.a.com/test/index/indexto access it;
2. CLI:
When you directly execute the PHP script through the command line, use php index. php m: test c: Index a: index under the Public Directory.
I used the framework to implement an MVC application. why not use the framework to build an MVC application?
Please stay tuned for the next lecture !!!!