2017-01-23 40 views
1

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

一切工作正常。

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

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

+0

看看这里:https://github.com/noodlehaus/dispatch。您可以使用这个库或查看代码。 –

+0

感谢您的链接。我会试试看。 – Harish

回答

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