Search code examples
php.htaccessroutescustom-routes

How to make my routes more efficient?


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

Everything is working fine.

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?


Solution

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