route = new Route(); // } public function index() { $doc = $GLOBALS['doc']; $doc = ltrim($doc, '/'); if (substr($doc, -1) === '/') { $doc = $doc . "index.md"; } // var_dump($doc); // exit; $doc = empty($doc) ? 'index.md' : $doc; if (pathinfo($doc,PATHINFO_EXTENSION) != 'md') { $doc = $doc . ".md"; } $filename = DATA_PATH . $doc; $text = ""; $toc = ""; $content = ""; if ($is_file_exists = file_exists($filename)) { $parsedownExtension = new \ParsedownExtension(); $parsedownExtension->setTocEnabled(true); $text = file_get_contents($filename); $res = $parsedownExtension->text($text); $toc = $res['toc']; $content = $res['content']; } $this->assign('doc',$doc); $this->assign('is_file_exists', $is_file_exists); $this->assign('text',$text); $this->assign('toc',$toc); $this->assign('content',$content); $this->fetch(); } public function editor() { $doc = $GLOBALS['doc']; var_dump($doc); exit; $this->fetch(); } public function save() { $doc = $_POST['doc']; $content = $_POST['content']; $pathinfo = pathinfo($doc); $dir_name = DATA_PATH . $pathinfo['dirname']; if (!is_dir($dir_name) && !mkdir($dir_name, 0744, true)) { die(json_encode(['code'=>2, 'msg'=>'目录创建失败'])); } $filename = DATA_PATH . $doc; $res = file_put_contents($filename, $content); if ($res === false) { echo json_encode(['code'=>2, 'msg'=>'保存失败']); } else { echo json_encode(['code'=>0, 'msg'=>'保存成功, 字数:' . $res]); } die(); } }