如何提高我的路线效率?

时间:2017-01-23 15:18:24

标签: php .htaccess routing custom-routes

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

有没有更好的方法来调用特定路由功能并防止不必要的呼叫?

2 个答案:

答案 0 :(得分:1)

如果网址匹配,您已经知道了。我会根据结果返回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'];
             });
}

答案 1 :(得分:1)

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