Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用正则表达式匹配URL路由_Regex_Url_Routing_Routes - Fatal编程技术网

Regex 使用正则表达式匹配URL路由

Regex 使用正则表达式匹配URL路由,regex,url,routing,routes,Regex,Url,Routing,Routes,我正在为课程的结束构建一个PHP框架,我一直在寻找一个解决方案来匹配一些自定义路由和标准路由 我的框架的路径与Zend framework 1的路径相似 这是比赛的标准路线 /module/controller/action/param/value/param2/value2/paramn/valuen URI的部分是可选的,/route导致应用程序模块、索引控制器和索引操作,而不带参数和值 我被困在一些自定义路线中,我这样定义: /blog/:postname/ /admin/logout/

我正在为课程的结束构建一个PHP框架,我一直在寻找一个解决方案来匹配一些自定义路由和标准路由

我的框架的路径与Zend framework 1的路径相似

这是比赛的标准路线

/module/controller/action/param/value/param2/value2/paramn/valuen
URI的部分是可选的,/route导致应用程序模块、索引控制器和索引操作,而不带参数和值

我被困在一些自定义路线中,我这样定义:

/blog/:postname/
/admin/logout/
/blog/posts/:year/:category/
/about/
路由必须与此示例URI请求匹配

/blog/my-first-post/
/blog/my-first-post/referenced/facebook/
/admin/logout/
/admin/logout/session-id/246753/action
/blog/posts/2013/turism/
/blog/posts/2013/turism/page/2/
但不必符合标准路线。自定义管线必须位于标准管线之前。 标准路线的一些示例。示例:

/
/application/
/application/index/
/application/index/index/
/blog/posts/view/id/3/
/admin/login/
/admin/login/logout (that one are the 
/admin/blog/posts/edit/id/3/
/admin/blog/posts/edit/id/3/success/false/
我发现在比赛中使用正则表达式是一种很好的方法,但我已经试着学习正则表达式一个多月了,但还没有完全掌握

PS:匹配当前路由后,我必须将:变量绑定到请求URI中的相关位置


谢谢你的帮助。

虽然诚然很吸引人,但在这种特殊情况下,我不会使用正则表达式。尽管我通常都是这样。一个简单的循环和匹配就可以了,除非你的课程设置了一些你必须遵循的限制

我放了一个例子,应该完成任务并在控制台中运行,只是为了说明我的意思

function get_route($uri){
  $routes = [
    'blog#show' => 'blog/:postname',
    'admin#logout' => 'admin/logout',
    'blog#category' => 'blog/posts/:year/:category',
    'home#about' => 'about'
  ];

  $params = [];

  $uri = preg_replace('/#|\?.+/', '', $uri); // remove hash or query strings
  $uri = preg_replace('/(^\/)?(\/$)?/', '', $uri); // trim slashes
  $uri = explode('/', $uri);
  $action = null;
  foreach ($routes as $this_action => $this_route) { // loop through possible routes
    $fractions = explode('/', $this_route);
    if (sizeof($fractions) !== sizeof($uri)) continue; // did not match length of uri
    for ($i=0; $i<sizeof($uri); $i++) { // compare each part of uri to each part of route
      if (substr($fractions[$i], 0, 1) !== ':' && $fractions[$i] !== $uri[$i]) break; // not a match and not a param
      if ($i === sizeof($uri)-1) { // made it to the last fraction!
        $ii = 0;
        foreach ($fractions as $fraction) {
          if (substr($fraction, 0, 1) == ':') { // it's a param, map it!
            $params[substr($fraction,1)] = $uri[$ii];
          }
          $ii++;
        }

        return ['action'=>$this_action, 'params'=>$params];
      }
    }
  }
  return false;
}
函数get_route($uri){ $routes=[ 'blog#show'=>'blog/:postname', “管理员#注销”=>“管理员/注销”, “blog#category”=>“blog/posts/:year/:category”, “主页#关于”=>“关于” ]; $params=[]; $uri=preg\u replace('/#| \?.+/','$uri);//删除哈希或查询字符串 $uri=preg\u replace('/(^\/)?(\/$)?/','','$uri);//修剪斜杠 $uri=分解('/',$uri); $action=null; foreach($this\u action=>$this\u route)遍历可能的路由 $sections=分解(“/”,$this_route); 如果(sizeof($sections)!==sizeof($uri))继续;//与uri的长度不匹配
对于($i=0;$i我可以用这段代码满足我的需求,已经通过了很多测试

public function matchCustomRoute($uri)
{
    if($uri == '')
    {
        return null;
    }

    $customRoutes = $this->getRoutes();

    $explodeUri = explode('/', $uri);
    $arrayUri = array();
    foreach($explodeUri as $uriPart)
    {
        if($uriPart == '')
        {
            continue;
        }
        $arrayUri[] = $uriPart;
    }
    $countUri = count($arrayUri);


    foreach($customRoutes as $key => $value)
    {

        $explodeRoute = explode('/',$value['route']);
        $arrayRoute = array();
        foreach($explodeRoute as $routePart)
        {
            if($routePart == '')
            {
                continue;
            }
            $arrayRoute[] = $routePart;
        }
        $countRoute = count($arrayRoute);
        if($countRoute > $countUri)
        {
            continue;
        }
        $matches = 0;

        for($i = 0 ; $i < $countRoute ; $i++)
        {
            $match = preg_match('/'.$arrayUri[$i].'/', '/'.$arrayRoute[$i].'/');
            if($match == 0)
            {
                if(substr($arrayRoute[$i], 0, 1) == ':')
                {
                    $value['params'][substr($arrayRoute[$i], 1)] = $arrayUri[$i];
                }
                else
                {
                    continue;
                }
            }
            $matches++;

        }
        if($matches == $countRoute)
        {
            return $value;
        }

    }
    return null;
}
公共函数matchCustomRoute($uri)
{
如果($uri='')
{
返回null;
}
$customRoutes=$this->getRoutes();
$explodeUri=explode(“/”,$uri);
$arrayUri=array();
foreach($uri作为$uriPart)
{
如果($uriPart=='')
{
继续;
}
$arrayUri[]=$uriPart;
}
$countUri=count($arrayUri);
foreach($customRoutes作为$key=>$value)
{
$explodeRoute=explode('/',$value['route']);
$arrayRoute=array();
foreach($routePart为$routePart)
{
如果($routePart=='')
{
继续;
}
$arrayRoute[]=$routePart;
}
$countRoute=count($arrayRoute);
如果($countRoute>$countUri)
{
继续;
}
$matches=0;
对于($i=0;$i<$countRoute;$i++)
{
$match=preg_match('/'.$arrayUri[$i].'/'.,'/'.$arrayRoute[$i]./');
如果($match==0)
{
如果(substr($arrayRoute[$i],0,1)=':')
{
$value['params'][substr($arrayRoute[$i],1)]=$arrayUri[$i];
}
其他的
{
继续;
}
}
$matches++;
}
如果($matches==$countRoute)
{
返回$value;
}
}
返回null;
}

谢谢你的帮助。

谢谢你的回答,但是..天哪,这几乎和我在寻找正则表达式之前尝试的方法一样。。