如何使我的路线更高效? [英] How to make my routes more efficient?

查看:100
本文介绍了如何使我的路线更高效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

By making rewrite directives in .htaccess file I write my custom directives.

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);
                    }

                }
            }
        }
    }
}

然后我启动并添加了一些路线,如下所示。

And then I initiated and added some routes as follows.

$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

But, every get function is calling instead of required one. To prevent this, I call die at the end of success callback of matched route.

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

Is there any better way to call particular route function and preventing unnecessary calls?

推荐答案

在阅读了Olaf的回答后,我找到了解决方案。
路由中添加 match_found 属性将有助于防止不必要的呼叫。

After reading Olaf's answer I found some solution. Adding match_found property to Route will help for preventing unnecessary calls.

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;
                    }
                }
            }
        }
    }

这篇关于如何使我的路线更高效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆