通过f3-Routing引擎替换URL中的模式

时间:2014-09-12 14:18:13

标签: php .htaccess mod-rewrite routing fat-free-framework

我正在处理基于平面文件的项目,并尝试从URL中删除特定模式。内容存储在带有markdown文件的"内容"目录中。我想要可排序的内容文件夹名称,如:


contentfolder

- 01-home

  - 01-subpage

  - 02-subpage2

- 02-page02

  - 01-subpage

  - 02-subpage2

等...

目前,网址看起来像这样:

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

这真是太丑了;)

我更愿意按以下方式获取网址:

http://domain.com/home/subpage

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

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


我的脚本目前使用的是f3-Wildcard-Solution(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;
}

谢谢你们!

1 个答案:

答案 0 :(得分:0)

这是一个解决方案,假设:

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

=>

$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();