Step-by-step authoring of PHP Framework (iii)

Source: Internet
Author: User
Tags define empty include php file php code php framework trim access

According to what I said last time, a basic MVC architecture has been built up, but there are many problems, such as: the model of each connection to DB, shut down the DB code redundancy, view this or PHP code, each file to use a lot of include code, and so on.

To write a framework, the first is to specify a standard directory structure, we temporarily set the directory structure to death, not allow users to modify, so as to reduce the amount of code, so that people can see more clearly.

Because Toper was written earlier, I'm going to use the Toper directory structure to do the demo:

Library (Frame storage directory)

Test (assuming this frame name is called test)

Userapps (user's App Store directory)

Configs (configuration file storage directory)

Modules (Module storage directory)

Controllers (All Controller storage directory)

Models (Model storage directory)

Views (View storage directory)

Helpers (secondary file directory)

Plugins (Plugin storage directory, temporarily not used)

Public (Web site root directory)

index.php (most important entry file)

This place needs to explain, the public directory is the root of the site, the Library directory and the Userapps directory is stored outside the root directory of the site, so that users can access through the browser can not directly access the PHP file, so as to improve the security of the site.

Before we put all the functionality of the route into the portal file, in fact, this is not appropriate, the entry file should not do these logical processing, so we put the code stored in the entry file into the library directory under the test directory, because the previous function in the entry file is routed, So we migrate the code to route.php, and this file completes the routing function.

Okay, let's take a look at the contents of the route.php file:

01 <?php
02 Class Route {
03 public static function run () {
04 $controller = Empty ($_get[' C '])? ' Index ': Trim ($_get[' C ']); The default controller is set
05 $action = Empty ($_get[' a '])? ' Index ': Trim ($_get[' a ']); Set the default action
06 $controllerBasePath = DirName (__file__). '/.. /.. /userapps/modules/controllers/';
07 $controllerFilePath = $controllerBasePath. $controller. ' controller.php ';
08 if (Is_file ($controllerFilePath)) {
09 Include $controllerFilePath;
10 $controllerName = $controller. ' Controller ';
11 if (class_exists ($controllerName)) {
12 $controllerHandler = new $controllerName ();
13 if (Method_exists ($controllerHandler, $action)) {
14 $controllerHandler-> $action ();
15 } else {
16 Echo ' The method does not exists ';
17 }
18 } else {
19 Echo ' The class does not exists ';
20 }
21st } else {
22 Echo ' controller not exists ';
23 }
24 }
25 }

At this point, see, we just copied the code from the entry file into the route.php file, so what's the benefit?

The entry file code is missing!!!

1 <?php
2 Include DirName (__file__). '/.. /library/test/route.php ';
3 Route::run ();

Only two lines of code, concise enough!!

But you see a problem does not, every time I need to use dirname to determine the current path, and if there are 20 files in this framework, each file has 3 use dirname to get the absolute path, then feel terrible, 90 repeat code, if the path of a modified, It's going to be your nightmare!!

So, we might as well define a path in the entry file as the base path for the app, and since we often use the test directory path below the library, we also set a framework path, so the entry file is modified to:

1 <?php
2 Defined (' App_path ') define (' App_path ', DirName (__file__). '/..');
3 Defined (' Framework_path ') define (' Framework_path ', App_path. '/library/test ');
4 Include Framework_path. '/route.php ';
5 Route::run ();

Then the corresponding route.php also need to be modified:

01 <?php
02 Class Route {
03 public static function run () {
04 $controller = Empty ($_get[' C '])? ' Index ': Trim ($_get[' C ']); The default controller is set
05 $action = Empty ($_get[' a '])? ' Index ': Trim ($_get[' a ']); Set the default action
06 $controllerBasePath = App_path. '/userapps/modules/controllers/';
07 $controllerFilePath = $controllerBasePath. $controller. ' controller.php ';
08 if (Is_file ($controllerFilePath)) {
09 Include $controllerFilePath;
10 $controllerName = $controller. ' Controller ';
11 if (class_exists ($controllerName)) {
12 $controllerHandler = new $controllerName ();
13 if (Method_exists ($controllerHandler, $action)) {
14 $controllerHandler-> $action ();
15 } else {
16 Echo ' The method does not exists ';
17 }
18 } else {
19 Echo ' The class does not exists ';
20 }
21st } else {
22 Echo ' controller not exists ';
23 }
24 }
25 }

Now look at this code is a lot better!!!

But through MVC to build the application, often a file include many classes, then whether the framework can implement automatic import classes, and do not use manual include, please continue to pay attention to my log!!



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.