通过f3路由引擎替换URL中的模式


Replace pattern in URL through f3-Routing engine

我在一个基于平面文件的项目中工作,我试图从URL中删除特定的模式。内容存储在带有markdown文件的"content"目录中。我想要可排序的内容文件夹名称,如:


内容文件夹

- 01-home
  - 01-subpage
  - 02-subpage2
- 02-page02
  - 01-subpage
  - 02-subpage2

等等。。。

目前,URL看起来像这样:

http://domain.com/01-home/02-subpage

这真的很难看;)

我更喜欢得到如下网址工作:

http://domain.com/home/subpage

我更喜欢一个解决方案,它适用于每种url的情况:

http://domain.com/home/subpage/subsubpage/subsubsubpage

等等。


我的脚本目前使用f3通配符解决方案(GET/*)。将替换请求的URL以获取内容。

function find($path = "") {
    $dirname = str_replace(globals::root(), "", globals::current());
    if ($dirname == "/") {
        $this->location = globals::content() . globals::home() . "/" . $path;
    } else {
        $this->location = globals::content() . $dirname . "/" . $path;
    }
    return $this;
}

谢谢你们!

这里有一个解决方案,假设:

  • 您的降价文件位于content/文件夹中
  • 每个子文件夹都以两位数字和一个连字符(XX之类的)为前缀

=>

$f3=require('lib/base.php');
$f3->set('CONTENT','content/');//location of markdown files
$f3->route('GET /*',function($f3,$params){
  $path=str_replace('..','',$params[1]);//security
  $dirs=glob($f3->get('CONTENT').'??-'.str_replace('/','/??-',$path));
  if ($dirs) {
    $dir=$dirs[0];//pick first match
    echo 'Markdown::instance()->convert($dir.'/default.md');
  } else
    $f3->error(404);
});
$f3->run();