目前在自學(xué)php,記錄下自己的學(xué)習(xí)過(guò)程姐直。
thinkphp 5.0自動(dòng)生成功能模塊及模塊中目錄文件有兩種方法:
第一種方案:
- 步驟一:在"build.php"文件中的ruturn方法中添加需要自動(dòng)生成的功能模塊兰伤。
return [
// 生成應(yīng)用公共文件
'__file__' => ['common.php', 'config.php', 'database.php'],
'__dir__' => ['runtime/cache', 'runtime/log', 'runtime/temp', 'runtime/template'],
// 定義demo模塊的自動(dòng)生成 (按照實(shí)際定義的文件名生成)
'demo' => [
//文件
'__file__' => ['common.php'],
//文件夾
'__dir__' => ['behavior', 'controller', 'model', 'view'],
//每個(gè)文件夾中的類文件
'controller' => ['Index', 'Test', 'UserType'],
'model' => ['User', 'UserType'],
'view' => ['index/index'],
],
// 其他更多的模塊定義
];
- 步驟二:在"public/index.php"中調(diào)用"\think\Build"類的方法自動(dòng)生成上面定義好的模塊灾搏。
// [ 應(yīng)用入口文件 ]
// 定義應(yīng)用目錄
define('APP_PATH', __DIR__ . '/../application/');
// 加載框架引導(dǎo)文件
require __DIR__ . '/../thinkphp/start.php';
//自動(dòng)生成定義的模塊
$build = include APP_PATH.'../build.php';
\think\Build::run($build);
-
步驟三:
運(yùn)行public中的index.php,成功之后生成的模塊在application中喝滞。運(yùn)行之后生成的功能模塊如下圖所示:
00.jpg
第二種方案:
第二種方案不依賴自動(dòng)生成文件(即“build.php”定義的模塊)专筷,直接使用默認(rèn)的目錄生成模塊强霎。在"public/index.php"中輸入如下代碼:
\think\Build::module('Person');
即可自動(dòng)生成Person模塊宣吱。如下圖所示:
01.jpg
總結(jié)
兩種方法相比而言,第一種方法較為靈活明未,可以自己來(lái)定義需要生成的功能模塊和類文件槽华。而第二種方法默認(rèn)生成MVC模塊,common.php,config.php,及controller中的index.php。