【问题标题】:How to make my routes more efficient?如何让我的路线更有效率?
【发布时间】:2017-06-08 03:47:08
【问题描述】:

通过在 .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

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

【问题讨论】:

标签: php .htaccess routing custom-routes


【解决方案1】:

您已经知道,如果 URL 匹配。我会根据结果返回truefalse,并决定返回代码,如果您需要更进一步,例如

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

【讨论】:

    【解决方案2】:

    阅读奥拉夫的回答后,我找到了一些解决方案。 将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;
                        }
                    }
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2021-04-28
      • 1970-01-01
      • 2016-03-01
      • 1970-01-01
      • 2015-01-24
      • 1970-01-01
      • 2012-04-01
      • 1970-01-01
      • 2011-10-03
      相关资源
      最近更新 更多