(1)首先做法參照:
thinkphp5的手冊的 命令行--->自動生成目錄結構
或者看雲的資料:https://www.kancloud.cn/manual/thinkphp5/118021
(2)build.php(位於我的框架的根目錄下)
build.php內容展示,其實沒啥變化
1 <?php 2 // +---------------------------------------------------------------------- 3 // | ThinkPHP [ WE CAN DO IT JUST THINK ] 4 // +---------------------------------------------------------------------- 5 // | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. 6 // +---------------------------------------------------------------------- 7 // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) 8 // +---------------------------------------------------------------------- 9 // | Author: liu21st <liu21st@gmail.com> 10 // +---------------------------------------------------------------------- 11 /** 12 * 對應那個thinkphp中的library中的think 的Build.php文件 13 */ 14 return [ 15 // 生成應用公共文件 16 '__file__' => ['common.php', 'config.php', 'database.php'], 17 18 // 定義demo模塊的自動生成 (按照實際定義的文件名生成) 19 'demo' => [ 20 '__file__' => ['common.php','config.php'], 21 '__dir__' => ['behavior', 'controller', 'model', 'view'], 22 'controller' => ['Index', 'Test', 'UserType'], 23 'model' => ['User', 'UserType'], 24 'view' => ['index/index'], 25 ], 26 // 其他更多的模塊定義 27 ];
觀察可知:__file__是單獨的,但是你看那個__dir__中的controler model view和下面的那個 controller model view是對應的 其實就是對應Build.php中的 建立目錄 監理文件和建立模板的函數對應
下面是那個Build.php文件代碼展示:
1 <?php 2 // +---------------------------------------------------------------------- 3 // | ThinkPHP [ WE CAN DO IT JUST THINK ] 4 // +---------------------------------------------------------------------- 5 // | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. 6 // +---------------------------------------------------------------------- 7 // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) 8 // +---------------------------------------------------------------------- 9 // | Author: liu21st <liu21st@gmail.com> 10 // +---------------------------------------------------------------------- 11 12 namespace think; 13 /** 14 * 這個是我自動創建類文件,詳細可以看手冊的命令行--->自動生成目錄結構 15 * run()方法,你傳入一個配置文件的返回值,就創建一個對應的目錄 16 * BuildDir()創建一個目錄,傳一個數組 17 *所有的參數參照build.php文件寫法 18 * 19 * 可以模仿這個建立模塊或者目錄的函數的寫法,自己再編寫幾個不一樣的創建東西的函數。 20 * Class Build 21 * @package think 22 */ 23 class Build 24 { 25 /** 26 * 根據傳入的build資料創建目錄和文件 27 * @access protected 28 * @param array $build build列表 29 * @param string $namespace 應用類庫命名空間 30 * @param bool $suffix 類庫后綴 31 * @return void 32 */ 33 public static function run(array $build = [], $namespace = 'app', $suffix = false) 34 { 35 // 鎖定 36 $lockfile = APP_PATH . 'build.lock'; 37 if (is_writable($lockfile)) { 38 return; 39 } elseif (!touch($lockfile)) { 40 throw new Exception('應用目錄[' . APP_PATH . ']不可寫,目錄無法自動生成!<BR>請手動生成項目目錄~', 10006); 41 } 42 foreach ($build as $module => $list) { 43 if ('__dir__' == $module) { 44 // 創建目錄列表 45 self::buildDir($list); 46 } elseif ('__file__' == $module) { 47 // 創建文件列表 48 self::buildFile($list); 49 } else { 50 // 創建模塊 51 self::module($module, $list, $namespace, $suffix); 52 } 53 } 54 // 解除鎖定 55 unlink($lockfile); 56 } 57 58 /** 59 * 創建目錄 60 * @access protected 61 * @param array $list 目錄列表 62 * @return void 63 */ 64 protected static function buildDir($list) 65 { 66 foreach ($list as $dir) { 67 if (!is_dir(APP_PATH . $dir)) { 68 // 創建目錄 69 mkdir(APP_PATH . $dir, 0755, true); 70 } 71 } 72 } 73 74 /** 75 * 創建文件 76 * @access protected 77 * @param array $list 文件列表 78 * @return void 79 */ 80 protected static function buildFile($list) 81 { 82 foreach ($list as $file) { 83 if (!is_dir(APP_PATH . dirname($file))) { 84 // 創建目錄 85 mkdir(APP_PATH . dirname($file), 0755, true); 86 } 87 if (!is_file(APP_PATH . $file)) { 88 file_put_contents(APP_PATH . $file, 'php' == pathinfo($file, PATHINFO_EXTENSION) ? "<?php\n" : ''); 89 } 90 } 91 } 92 93 /** 94 * 創建模塊 95 * @access public 96 * @param string $module 模塊名 97 * @param array $list build列表 98 * @param string $namespace 應用類庫命名空間 99 * @param bool $suffix 類庫后綴 100 * @return void 101 */ 102 public static function module($module = '', $list = [], $namespace = 'app', $suffix = false) 103 { 104 $module = $module ? $module : ''; 105 if (!is_dir(APP_PATH . $module)) { 106 // 創建模塊目錄 107 mkdir(APP_PATH . $module); 108 } 109 if (basename(RUNTIME_PATH) != $module) { 110 // 創建配置文件和公共文件 111 self::buildCommon($module); 112 // 創建模塊的默認頁面 113 self::buildHello($module, $namespace, $suffix); 114 } 115 if (empty($list)) { 116 // 創建默認的模塊目錄和文件 117 $list = [ 118 '__file__' => ['config.php', 'common.php'], 119 '__dir__' => ['controller', 'model', 'view'], 120 ]; 121 } 122 // 創建子目錄和文件 123 foreach ($list as $path => $file) { 124 $modulePath = APP_PATH . $module . DS; 125 if ('__dir__' == $path) { 126 // 生成子目錄 127 foreach ($file as $dir) { 128 self::checkDirBuild($modulePath . $dir); 129 } 130 } elseif ('__file__' == $path) { 131 // 生成(空白)文件 132 foreach ($file as $name) { 133 if (!is_file($modulePath . $name)) { 134 file_put_contents($modulePath . $name, 'php' == pathinfo($name, PATHINFO_EXTENSION) ? "<?php\n" : ''); 135 } 136 } 137 } else { 138 // 生成相關MVC文件 139 foreach ($file as $val) { 140 $val = trim($val); 141 $filename = $modulePath . $path . DS . $val . ($suffix ? ucfirst($path) : '') . EXT; 142 $space = $namespace . '\\' . ($module ? $module . '\\' : '') . $path; 143 $class = $val . ($suffix ? ucfirst($path) : ''); 144 switch ($path) { 145 case 'controller': // 控制器 146 $content = "<?php\nnamespace {$space};\n\nclass {$class}\n{\n\n}"; 147 break; 148 case 'model': // 模型 149 $content = "<?php\nnamespace {$space};\n\nuse think\Model;\n\nclass {$class} extends Model\n{\n\n}"; 150 break; 151 case 'view': // 視圖 152 $filename = $modulePath . $path . DS . $val . '.html'; 153 self::checkDirBuild(dirname($filename)); 154 $content = ''; 155 break; 156 default: 157 // 其他文件 158 $content = "<?php\nnamespace {$space};\n\nclass {$class}\n{\n\n}"; 159 } 160 161 if (!is_file($filename)) { 162 file_put_contents($filename, $content); 163 } 164 } 165 } 166 } 167 } 168 169 /** 170 * 創建模塊的歡迎頁面 171 * @access public 172 * @param string $module 模塊名 173 * @param string $namespace 應用類庫命名空間 174 * @param bool $suffix 類庫后綴 175 * @return void 176 */ 177 protected static function buildHello($module, $namespace, $suffix = false) 178 { 179 $filename = APP_PATH . ($module ? $module . DS : '') . 'controller' . DS . 'Index' . ($suffix ? 'Controller' : '') . EXT; 180 if (!is_file($filename)) { 181 $content = file_get_contents(THINK_PATH . 'tpl' . DS . 'default_index.tpl'); 182 $content = str_replace(['{$app}', '{$module}', '{layer}', '{$suffix}'], [$namespace, $module ? $module . '\\' : '', 'controller', $suffix ? 'Controller' : ''], $content); 183 self::checkDirBuild(dirname($filename)); 184 file_put_contents($filename, $content); 185 } 186 } 187 188 /** 189 * 創建模塊的公共文件 190 * @access public 191 * @param string $module 模塊名 192 * @return void 193 */ 194 protected static function buildCommon($module) 195 { 196 $filename = CONF_PATH . ($module ? $module . DS : '') . 'config.php'; 197 198 self::checkDirBuild(dirname($filename)); 199 if (!is_file($filename)) { 200 file_put_contents($filename, "<?php\n//配置文件\nreturn [\n\n];"); 201 } 202 $filename = APP_PATH . ($module ? $module . DS : '') . 'common.php'; 203 if (!is_file($filename)) { 204 file_put_contents($filename, "<?php\n"); 205 } 206 } 207 208 protected static function checkDirBuild($dirname) 209 { 210 if (!is_dir($dirname)) { 211 mkdir($dirname, 0755, true); 212 } 213 } 214 }
(3)然后在index.php文件中的寫法展示:
1 <?php 2 // +---------------------------------------------------------------------- 3 // | ThinkPHP [ WE CAN DO IT JUST THINK ] 4 // +---------------------------------------------------------------------- 5 // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved. 6 // +---------------------------------------------------------------------- 7 // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) 8 // +---------------------------------------------------------------------- 9 // | Author: liu21st <liu21st@gmail.com> 10 // +---------------------------------------------------------------------- 11 12 // [ 應用入口文件 ] 13 define('CONF_PATH',__DIR__.'./config/'); 14 // 定義應用目錄 15 define('APP_PATH', __DIR__.'./application/'); 16 //define('STATIC','/Per_boke/public/static'); 17 // 加載框架引導文件 18 require __DIR__.'./thinkphp/start.php'; 19 /** 20 * 下面是自己定義的配置文件的目錄 21 */ 22 $build=include './build.php'; 23 \think\Build::run($build);
詳細講解可看:http://pan.baidu.com/s/1jIx47fK 密碼:hvje