如何让我的路线更有效率?

How to make my routes more efficient?

通过在 .htaccess 文件中重写指令,我编写了自定义指令。

class Route {

    public $request_url;
    public $url_args;
    public $request_method;

    function __construct() {
        $this -> request_url = $_SERVER['PATH_INFO'];
        $this -> url_args = explode('/', substr($_SERVER['PATH_INFO'], 1));
        $this -> request_method = $_SERVER['REQUEST_METHOD'];
    }

    public function get($url, $callback) {
        if($this -> request_method == 'GET') {
            if($url == $this -> request_url){
                $callback();
            } elseif(strpos($url, ':')) {
                $new_url_args = explode('/', substr($url, 1));
                if(count($new_url_args) == count($this -> url_args)) {
                    $match = true;
                    $args = [];

                    for($i = 0; $i < count($this -> url_args); $i++) {
                        if($new_url_args[$i][0] == ':') {
                            $args[substr($new_url_args[$i], 1)] = $this -> url_args[$i];
                        } else {
                            if($new_url_args[$i] != $this -> url_args[$i]){
                                $match = false;
                                break;
                            }
                        }
                    }

                    if($match) {
                        $callback($args);
                    }

                }
            }
        }
    }
}

然后我发起并添加了一些路由如下。

$route = new Route();

$route -> get('/', function() {
    echo "Welcome to DocsApp";
    die;
});

$route -> get('/test/sample', function() {
    echo 'tested';
    die;
});

$route -> get('/user/:id/:name', function($args) {
    echo  $args['id'] . '<br />' . $args['name'];
    die;
});

一切正常。

但是,每个 get 函数都在调用而不是必需的函数。 为了防止这种情况,我在匹配路由的成功回调结束时调用 die

有没有更好的方法来调用特定的路由函数并防止不必要的调用?

您已经知道 URL 是否匹配。我只是 return truefalse 取决于结果,并决定 return 代码,如果你需要更进一步,例如

public function get($url, $callback) {
    if ($this->request_method == 'GET') {
        if ($url == $this->request_url) {
            $callback();
            return true;
        } elseif (strpos($url, ':')) {
            $new_url_args = explode('/', substr($url, 1));
            if (count($new_url_args) == count($this->url_args)) {
                $match = true;
                // ...
                if($match) {
                    $callback($args);
                    return true;
                }
            }
        }
    }

    return false;
}
$route = new Route();

$match = $route->get('/', function() {
    echo "Welcome to DocsApp";
});

if (!$match) {
    $match = $route->get('/test/sample', function() {
                 echo 'tested';
             });
}

if (!$match) {
    $match = $route->get('/user/:id/:name', function($args) {
                 echo  $args['id'] . '<br />' . $args['name'];
             });
}

在阅读了 Olaf 的回答后,我找到了一些解决方案。 添加 match_found 属性 到 Route 将有助于防止不必要的调用。

class Route {

    public $request_url;
    public $url_args;
    public $request_method;
    private $match_found = false;

    function __construct() {
        $this -> request_url = $_SERVER['PATH_INFO'];
        $this -> url_args = explode('/', trim($_SERVER['PATH_INFO'], '/'));
        $this -> request_method = $_SERVER['REQUEST_METHOD'];
    }

    public function get($url, $callback) {
        if($this -> match_found) {
            return;
        }
        if($this -> request_method == 'GET') {
            if($url == $this -> request_url){
                $callback();
                $this -> match_found = true;
            } elseif(strpos($url, ':')) {
                $new_url_args = explode('/', trim($url, '/'));
                if(count($new_url_args) == count($this -> url_args)) {
                    $match = true;
                    $args = [];

                    for($i = 0; $i < count($this -> url_args); $i++) {
                        if($new_url_args[$i][0] == ':') {
                            $args[trim($new_url_args[$i], ':')] = $this -> url_args[$i];
                        } else {
                            if($new_url_args[$i] != $this -> url_args[$i]){
                                $match = false;
                                break;
                            }
                        }
                    }
                    if($match) {
                        $callback($args);
                        $this -> match_found = true;
                    }
                }
            }
        }
    }