url路由正则表达式PHP

时间:2012-07-30 13:36:37

标签: php regex routing

我正在编写一个处理PHP Web服务路由的类,但是我需要更正正则表达式,我想知道解析url的最有效方法是什么。

网址示例:

  • POST / users
  • GET / users
  • GET / users& limit = 10& offset = 0
  • GET / users / search& keyword = Richard
  • GET / users / 15 / posts / 38

我想在PHP中为类创建的是:

$router = new Router();
$router->addRoute('POST', '/users', function(){});
$router->addRoute('GET', '/users/:uid/posts/:pid', function($uid, $pid){});
$target = $router->doRouting();

目标变量现在包含一个数组:

  • 方法
  • URL
  • 回调方法

这是我到目前为止所得到的:

class Router{
    use Singleton;

    private $routes = [];
    private $routeCount = 0;

    public function addRoute($method, $url, $callback){
        $this->routes[] = ['method' => $method, 'url' => $url, 'callback' => $callback];
        $this->routeCount++;
    }

    public function doRouting(){
        $reqUrl = $_SERVER['REQUEST_URI'];
        $reqMet = $_SERVER['REQUEST_METHOD'];

        for($i = 0; $i < $this->routeCount; $i++){
            // Check if the url matches ...
            // Parse the arguments of the url ...
        }
    }
}

所以我首先需要一个正则表达式:

  1. / mainAction /:argumentName / secondaryAction /:secondaryActionName
  2. 检查是否与$ reqUrl匹配(参见上面的for循环)

    1. 提取参数,以便我们可以在回调函数中使用它们。
    2. 我自己尝试了什么:

      (code should be in the for loop @ doRouting function)
      
      // Extract arguments ...
      $this->routing[$i]['url'] = str_replace(':arg', '.+', $this->routing[$i]['url']);
      
      // Does the url matches the routing url?
      if(preg_match('#^' . $this->routes[$i]['url'] . '$#', $reqUrl)){
          return $this->routes[$i];
      }
      

      我非常感谢所有的帮助,非常感谢。

2 个答案:

答案 0 :(得分:9)

这基本上现在有效。

public function doRouting(){
    // I used PATH_INFO instead of REQUEST_URI, because the 
    // application may not be in the root direcory
    // and we dont want stuff like ?var=value
    $reqUrl = $_SERVER['PATH_INFO'];
    $reqMet = $_SERVER['REQUEST_METHOD'];

    foreach($this->routes as  $route) {
        // convert urls like '/users/:uid/posts/:pid' to regular expression
        $pattern = "@^" . preg_replace('/\\\:[a-zA-Z0-9\_\-]+/', '([a-zA-Z0-9\-\_]+)', preg_quote($route['url'])) . "$@D";
        $matches = Array();
        // check if the current request matches the expression
        if($reqMet == $route['method'] && preg_match($pattern, $reqUrl, $matches)) {
            // remove the first match
            array_shift($matches);
            // call the callback with the matched positions as params
            return call_user_func_array($route['callback'], $matches);
        }
    }
}

PS:您不需要$routeCount属性

答案 1 :(得分:0)

很好的答案@MarcDefiant。我遇到的最干净的 PHP 路由器。做了一个小的修改以支持正则表达式。不知道为什么要使用 preq_quote ?

小待办事项是将数组转换为关联数组。例如。将 ['0' => 1] 替换为 ['id' => 1]

function matchRoute($routes = [], $url = null, $method = 'GET')
{
    // I used PATH_INFO instead of REQUEST_URI, because the 
    // application may not be in the root direcory
    // and we dont want stuff like ?var=value
    $reqUrl = $url ?? $_SERVER['PATH_INFO'];
    $reqMet = $method ?? $_SERVER['REQUEST_METHOD'];

    $reqUrl = rtrim($reqUrl,"/");

    foreach ($routes as $route) {
        // convert urls like '/users/:uid/posts/:pid' to regular expression
        // $pattern = "@^" . preg_replace('/\\\:[a-zA-Z0-9\_\-]+/', '([a-zA-Z0-9\-\_]+)', preg_quote($route['url'])) . "$@D";
        $pattern = "@^" . preg_replace('/:[a-zA-Z0-9\_\-]+/', '([a-zA-Z0-9\-\_]+)', $route['url']) . "$@D";
        // echo $pattern."\n";
        $params = [];
        // check if the current request params the expression
        $match = preg_match($pattern, $reqUrl, $params);
        if ($reqMet == $route['method'] && $match) {
            // remove the first match
            array_shift($params);
            // call the callback with the matched positions as params
            // return call_user_func_array($route['callback'], $params);
            return [$route, $params];
        }
    }
    return [];
}


$match = matchRoute([
    [
        'method' => 'GET',
        'url' => '/:id',
        'callback' => function($req) {
            exit('Hello');
        }
    ],
    [
        'method' => 'GET',
        'url' => '/api/(.*)', // Match all /api/hello/test/...
        'callback' => function($req) {
            print_r($req);
            exit('cool');
        }
    ]
]);

list($route,$params) = $match;

call_user_func_array($route['callback'], [$params]);