Home  >  Article  >  Backend Development  >  PHP simply implements routing Route function

PHP simply implements routing Route function

Guanhui
Guanhuiforward
2020-04-30 09:41:0912646browse

For independent PHP frameworks written in native PHP, use a single entry file to achieve path access. The primary problem we will encounter at this time is: mutual inclusion of files, followed by routing distribution. When we do not use the mature PHP framework for web development, we will find that the above two problems will be very troublesome for us.

Problem solution:

1. File contains

There are two ways to include files in php: require and include. The difference between the two methods When an error occurs when using require to include a file, a serious error will be reported and the PHP script will stop running; when an error occurs when using include to include a file, a warning will appear, but the PHP script will continue to execute. At the same time, both methods include the corresponding xxx_once method, which can avoid the "declare class" problem. Therefore, when generally using file inclusion, we try to use include_once to include the file.

At the same time, regarding the path issue included in the file, we can use the method of setting global variables to find other path files based on the absolute path of the entry file. Modifying the include_path path in php.ini is of course another way. Use require and include to include the file. If it is a relative path, it will first be found according to the include_path setting in the php.ini configuration file.

2. Route distribution

PHP-based route distribution is essentially using the path in the URL to match the corresponding control class and calling the methods in it. Processing of related operations.

The code without saying a word:

<?php
/**
 * Author:helen
 * CreateTime: 2016/07/27 10:26
 * Description:
 */
// 权限控制
include_once &#39;./auth.php&#39;;
// 应用入口文件
date_default_timezone_set("Asia/Shanghai");
header(&#39;Content-type: text/html;charset=utf-8&#39;);
// 项目根路径
define(&#39;BASEPATH&#39;, dirname(__FILE__));
// 调试模式
define(&#39;APP_DEBUG&#39;, True);
// 引入配置文件
include_once BASEPATH . &#39;/config/config.php&#39;;
// 路由控制
$router = include_once BASEPATH . &#39;/config/router.php&#39;;
if ($_SERVER[&#39;HTTP_HOST&#39;] !== &#39;xxx.com&#39;) {
    var_dump(&#39;当前host不被允许&#39;);
} else {
    $request_path = str_replace(&#39;/index.php&#39;, &#39;&#39;, $_SERVER[&#39;PHP_SELF&#39;]);
    $request_query = getCurrentQuery();
    if (array_key_exists($request_path, $router)) {
        $module_file = BASEPATH . $router[$request_path][&#39;file_name&#39;];
        $class_name = $router[$request_path][&#39;class_name&#39;];
        $method_name = $router[$request_path][&#39;method_name&#39;];
        if (file_exists($module_file)) {
            include $module_file;
            $obj_module = new $class_name();
            if (!method_exists($obj_module, $method_name)) {
                die("要调用的方法不存在");
            } else {
                if (is_callable(array($obj_module, $method_name))) {
                    $obj_module->$method_name($request_query, $_POST);
                }
            }
        } else {
            die("定义的模块不存在");
        }
    } else {
        echo &#39;页面不存在&#39;;
    }
}

Using the above method, the routing distribution of the basic PHP framework can be realized.

Recommended tutorial: "PHP Tutorial"

The above is the detailed content of PHP simply implements routing Route function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete