The concept of the framework may have frightened a lot of PHP's coder in the beginning, in fact, you just want to understand a framework, in fact, there is nothing, but other people more complex functions, details to consider more, code quality is higher.
One of the most important files in the framework is the entry file, which basically corresponds to a master switch, and all requests need to go through this file:
2 |
Define (' App_path ', DirName (__file__). /..'); Apply Path |
3 |
Include App_path. ' /library/toper/core/frontcontroller.class.php ';//import Front Controller |
4 |
$frontController = Tp_frontcontroller::getinstance (); |
5 |
$frontController->run (); |
The file can only do one thing, which is to give control to the framework.
As we all know, an MVC application, all requests must go through the controller, so let's write a simpler 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, so that you can greatly reduce your own workload, and according to the specification, The function name is also meaningful, as the function that is written just now means to access the test module Indexcontrller The index This action under this controller.
So how do you use the model? It's actually very simple.
2 |
Class Test_indexmodel extends Tp_model { |
3 |
Public Function test () { |
The principle is the same as the controller, which is also the class of the inheritance framework, and then the custom method, where the method name is not limited and can be arbitrary.
Then write the model how to call in the controller, in fact, the method is to instantiate the model class in the controller, and then call the appropriate method, so modify just this controller's class:
2 |
Class Test_indexcontroller extends Tp_controller { |
3 |
Public Function indexaction () { |
4 |
$model = new Test_indexmodel (); |
Why are there no include statements?
That's because the framework helps you do all of this, of course, the framework is not omnipotent, when you are more familiar with, you can choose not to use automatic import.
What we did just now does not have a view, then how to write a view, is actually an HTML file, the framework in view this layer is generally the implementation of tag library, tag library to help you complete some of the original need to use PHP code to complete things, such as loop traversal, Without a tag library, you can only use <?php foreach (...) in this view file, which is bad for code separation.
If you have no concept of the tag library, you can look at the following code directly!
6 |
<print name = "Hello world!" type = "str"/> |
This is a little difficult code, the only difficulty may be the print label, which is defined by the framework, its function is very simple, is to print a string, you may feel that this is not meaningful, because I use the PHP code can easily complete this function, But you want to be the next one to do the front-end why also need to understand PHP, if the use of tag library, then front-end developers can use the same as HTML tags to manipulate the data.
We know that all requests must go through the controller, so the external can not directly access the view file, so you need to modify the controller's code!!!
2 |
Class Test_indexcontroller extends Tp_controller { |
3 |
Public Function indexaction () { |
4 |
$model = new Test_indexmodel (); |
6 |
$this->_display (' test.test '); |
If you've studied smarty, you might be familiar with the display method, which actually functions as a template file!!!
OK, a basic MVC application is set up, not difficult!!!
There are two ways to view the effects:
1.CGI:
Open the browser, browser access, if your domain name is: localhost/testframework, then you can use localhost/testframework/public/index.php/test/index/ Index to access, if you set up a virtual host, such as www.a.com, then only need to use www.a.com/Test/Index/index can be accessed;
2.CLI:
When you execute a PHP script directly from the command line, use PHP index.php m:test c:index A:index under the public directory.
Just using the framework to implement an MVC application, why not use a framework to build an MVC application?
Next time, keep your eye on!!!!.