Step-by-step authoring of PHP framework (i)

Source: Internet
Author: User
Tags functions html tags include php code php foreach php framework php script access

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:

1 <?php
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:

1 <?php
2 Class Test_indexcontroller extends Tp_controller {
3 Public Function indexaction () {
4 Echo ' a ';
5 }
6 }

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.

1 <?php
2 Class Test_indexmodel extends Tp_model {
3 Public Function test () {
4 return ' test ';
5 }
6 }

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:

1 <?php
2 Class Test_indexcontroller extends Tp_controller {
3 Public Function indexaction () {
4 $model = new Test_indexmodel ();
5 echo $model->test ();
6 }
7 }

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!

1
2
3 <title>test</title>
4
5 <body>
6 <print name = "Hello world!" type = "str"/>
7 </body>
8

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!!!

1 <?php
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 ');
7 }
8 }

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!!!!.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.