hinkphp5 官方提供了自动生成目录功能,可以用来自动生成需要的模块及目录结构和文件等,自动生成主要调用\think\Build类库。 默认的框架的根目录下面自带了一个build.php示例参考文件,内容如下: ~~~ return [ // 生成运行时目录 '__file__' => ['common.php'], // 定义index模块的自动生成 'index' => [ '__file__' => ['common.php'], '__dir__' => ['behavior', 'controller', 'model', 'view'], 'controller' => ['Index', 'Test', 'UserType'], 'model' => [], 'view' => ['index/index'], ], // 。。。 其他更多的模块定义 ]; ~~~ 可以给每个模块定义需要自动生成的文件和目录,以及MVC类。 * * dir 表示生成目录(支持多级目录) * file 表示生成文件(不定义默认会生成 config.php 文件) * controller 表示生成controller类 * model表示生成model类 * view表示生成html文件(支持子目录) 自动生成以APP_PATH为起始目录,dir 和 file 表示需要自动创建目录和文件,其他的则表示为模块自动生成。 模块的自动生成则以 APP_PATH.'模块名/' 为起始目录。 并且会自动生成模块的默认的Index访问控制器文件用于显示框架的欢迎页面。 因此,我们在 build.php 中做如下配置: ~~~ <?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <liu21st@gmail.com> // +---------------------------------------------------------------------- return [ // 生成应用公共文件 '__file__' => ['common.php', 'config.php', 'database.php'], // 定义demo模块的自动生成 (按照实际定义的文件名生成) 'demo' => [ '__file__' => ['common.php'], '__dir__' => ['behavior', 'controller', 'model', 'view'], 'controller' => ['Index', 'Test', 'UserType'], 'model' => ['User', 'UserType'], 'view' => ['index/index'], ], // 其他更多的模块定义 // 商城模块 'shop' => [ '__file__' => ['common.php', 'config.php'], '__dir__' => ['behavior', 'controller', 'model', 'view'], 'controller' => ['Index'], 'model' => [], 'view' => ['index/index'], ], ]; ~~~ >[warning] 运行下面的命令之前,确保你的 php命令行模式 可以运行。 > 首先进入系统的根目录,默认会读取应用目录application下面的build.php 作为自动生成的定义文件,如果你的定义文件位置不同,则需要使用--config参数指定如下: ~~~ >php think build --config build.php ~~~ 表示读取根目录下的build.php文件。 输入上面的命令,你会看到:Successed ![](https://box.kancloud.cn/b5d3472da006fceab696b78778a8a0d5_592x117.png) 此时,打开application目录就可以看到创建的模块了 ![](https://box.kancloud.cn/7b9d7b441f9aa8c2477afda0c9974d08_528x276.png)