Browse Source

文件路径及路由调整

huwhois 3 years ago
parent
commit
8394b33354
2 changed files with 42 additions and 18 deletions
  1. 23 8
      application/App.php
  2. 19 10
      application/Index.php

+ 23 - 8
application/App.php

@ -26,14 +26,15 @@ class App{
26 26
            $pathinfo = strpos($_SERVER['REQUEST_URI'], '?') ? strstr($_SERVER['REQUEST_URI'], '?', true) : $_SERVER['REQUEST_URI'];
27 27
        }
28 28
29
        $action = 'index';
29
        $action = '';
30
        $doc = '';
30 31
31 32
        if ($pathinfo) {
32
            $pathArr = explode('/',trim($pathinfo,'/'));//数组
33
            if (isset($pathArr[0])){
34
                $action = camelize($pathArr[0]);
35
                unset($pathArr[0]);
36
            }
33
            // $pathArr = explode('/',trim($pathinfo,'/'));//数组
34
            // if (isset($pathArr[0])){
35
            //     $action = camelize($pathArr[0]);
36
            //     unset($pathArr[0]);
37
            // }
37 38
            //url 多余部分转换成GET eg:index/id/1 实现get传值
38 39
            // $count = count($pathArr) + 1;
39 40
@ -44,13 +45,27 @@ class App{
44 45
            //     }
45 46
            //     $i = $i + 2;
46 47
            // };
47
            if (isset($pathArr[1])) {
48
                $_GET['doc'] = $pathArr[1];
48
            // foreach ($pathArr as $value) {
49
            //     $doc .= '/' . $value;
50
            // }
51
            // if (isset($pathArr[1])) {
52
            //     $_GET['doc'] = $pathArr[1];
53
            // }
54
55
            // 提取 action, 多余部分转为 markdown 文件路径
56
            $pathinfo = ltrim($pathinfo, '/');
57
            
58
            if ($index = strpos($pathinfo, '/')) {
59
                $action = substr($pathinfo,0,$index);
60
            } else {
61
                $action = $pathinfo;
49 62
            }
63
            $doc = str_replace($action, '', $pathinfo);
50 64
        }
51 65
        $action = $action ? : "index";
52 66
        
53 67
        $GLOBALS['action'] = $action;
68
        $GLOBALS['doc'] = $doc;
54 69
        
55 70
        return $action;
56 71
    }

+ 19 - 10
application/Index.php

@ -7,19 +7,23 @@ use app\Route;
7 7
class Index extends View
8 8
{
9 9
10
	public function __construct(){
11
		// $this->route = new Route();
12
	}
10
	// public function __construct(){
11
	// 	// $this->route = new Route();
12
	// }
13 13
 
14 14
    public function index()
15 15
    {
16
        $doc = isset($_GET['doc']) ? $_GET['doc'] : 'index.md';
17
        
16
        $doc = $GLOBALS['doc'];
17
        $doc = ltrim($doc, '/');
18
        if (substr($doc, -1) === '/') {
19
            $doc = $doc . "index.md";
20
        }
21
        $doc = empty($doc) ? 'index.md' : $doc;
18 22
        if (pathinfo($doc,PATHINFO_EXTENSION) != 'md') {
19 23
            $doc = $doc . ".md";
20 24
        }
21 25
22
        $filename = DATA_PATH . DS . $doc;
26
        $filename = DATA_PATH . $doc;
23 27
        $text = "";
24 28
        $toc = "";
25 29
        $content = "";
@ -47,11 +51,16 @@ class Index extends View
47 51
        $doc = $_POST['doc'];
48 52
        $content = $_POST['content'];
49 53
50
        $filename = DATA_PATH . DS . $doc;
51
        
52
        // var_dump($filename);
53
        // exit;
54
        $pathinfo = pathinfo($doc);
54 55
        
56
        $dir_name = DATA_PATH . $pathinfo['dirname'];
57
58
        if (!is_dir($dir_name) && !mkdir($dir_name, '0755', true)) {
59
            die(json_encode(['code'=>2, 'msg'=>'目录创建失败']));
60
        }
61
62
        $filename = DATA_PATH . $doc;
63
55 64
        $res = file_put_contents($filename, $content);
56 65
57 66
        if ($res === false) {