命令行模式_thinkphp 基于cli命令行模式 实现一键CURD操作

本文介绍了如何在ThinkPHP框架中利用CLI命令行模式实现一键创建、更新、读取和删除(CURD)操作。详细讲解了配置controller、model、validate文件的内容,并提供了查看和执行生成命令的方法。
摘要由CSDN通过智能技术生成

bac82e113625ca8a862ad6d60d428206.png

Chapter 1

利用tp模式中的cli命令行来实现生成curd文件

文档说明地址:自定义指令

生成命令行文件

php think make:command Curd curd

查看是否生成成功,并且成功运行

php think curd

Chapter 2

现在我们配置controller,model,view中的模板文件内容

907a427ba87c01d857888350a4c6f11a.png

controller 内容

<?php /**
* Created by.
* Date: 2020/9/28
* Time: 14:31
*/namespace app\$module\controller;use think\Controller;class $controller extends Controller
{
}复制代码

model 文件内容

<?php /**
* Created by.
* Date: 2020/9/28
* Time: 14:31
*/namespace app\$module\model;use think\Model;class $model extends Model
{
}复制代码

Validate文件内容

<?php /**
* Created by.
* Date: 2020/9/28
* Time: 14:48
*/namespace app\$module\validate;use think\Validate;class $validate extends Validate
{
}复制代码

View 待定·····

····

Chapter 3

<?php /**
* Created by.
* Date: 2020/9/28
* Time: 14:31
*/namespace app\command;use think\console\Command;use think\console\Input;use think\console\input\Argument;use think\console\input\Option;use think\console\Output;class Curd extends Command
{protected $appPath;// view 默认的三个模板protected $views = ['index', 'create', 'edit'];public function __construct()
{parent::__construct();$this->appPath = env('app_path');
}protected function configure()
{$this->setName('make:curd')
->addArgument('parameter', Argument::OPTIONAL, "parameter name")
->addOption('module', null, Option::VALUE_REQUIRED, 'module name')
->setDescription('Create curd option parameter model --module?');
}protected function execute(Input $input, Output $output)
{// 首先获取默认模块
$moduleName = config('app.default_module');
$parameterName = trim($input->getArgument('parameter'));if (!$parameterName) {
$output->writeln('parameter Name Must Set');exit;
}if ($input->hasOption('module')) {
$moduleName = $input->getOption('module');
}$this->makeController($parameterName, $moduleName);$this->makeModel($parameterName, $moduleName);$this->makeView($parameterName, $moduleName);$this->makeValidate($parameterName, $moduleName);
$output->writeln($parameterName . ' parameter create success');
$output->writeln($parameterName . ' model create success');
$output->writeln($parameterName . ' view create success');
$output->writeln($parameterName . ' validate create success');
}// 创建控制器文件protected function makeController($controllerName, $moduleName)
{
$controllerStub = $this->appPath . 'command' . DIRECTORY_SEPARATOR . 'curd' .DIRECTORY_SEPARATOR. 'Controller.stub';
$controllerStub = str_replace(['$controller', '$module'], [ucfirst($controllerName), strtolower($moduleName)], file_get_contents($controllerStub));
$controllerPath = $this->appPath . $moduleName . DIRECTORY_SEPARATOR . 'controller' . DIRECTORY_SEPARATOR;if (!is_dir($controllerPath)) {
mkdir($controllerPath, 0777, true);
}return file_put_contents( $controllerPath . ucfirst($controllerName) . '.php', $controllerStub);
}// 创建模型文件public function makeModel($modelName, $moduleName)
{
$modelStub = $this->appPath . 'command' . DIRECTORY_SEPARATOR . 'curd' . DIRECTORY_SEPARATOR . 'Model.stub';
$modelPath = $this->appPath . $moduleName . DIRECTORY_SEPARATOR . 'model';if (!is_dir($modelPath)) {
mkdir($modelPath, 0777, true);
}
$modelStub = str_replace(['$model', '$module'], [ucfirst($modelName), strtolower($moduleName)], file_get_contents($modelStub));return file_put_contents($modelPath . DIRECTORY_SEPARATOR . ucfirst($modelName) . '.php', $modelStub);
}// 创建验证器文件public function makeValidate($validateName, $moduleName)
{
$modelStub = $this->appPath . 'command' . DIRECTORY_SEPARATOR . 'curd' . DIRECTORY_SEPARATOR . 'Validate.stub';
$modelPath = $this->appPath . $moduleName . DIRECTORY_SEPARATOR . 'validate';if (!is_dir($modelPath)) {
mkdir($modelPath, 0777, true);
}
$modelStub = str_replace(['$validate', '$module'], [ucfirst($validateName), strtolower($moduleName)], file_get_contents($modelStub));return file_put_contents($modelPath . DIRECTORY_SEPARATOR . ucfirst($validateName) . '.php', $modelStub);
}// 创建模板public function makeView($parameterName, $moduleName)
{// 创建视图路径
$viewPath = (config('template.view_path') ? config('template.view_path') . $moduleName . DIRECTORY_SEPARATOR : env('app_path') . $moduleName . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR) . strtolower($parameterName);if (!is_dir($viewPath)) {
mkdir($viewPath, 0777, true);
}foreach ($this->views as $view) {// 视图模板源文件
$viewStub = $this->appPath . 'command' . DIRECTORY_SEPARATOR . 'curd' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . $view.'.stub';// 创建文件
file_put_contents($viewPath . DIRECTORY_SEPARATOR . $view . '.html', file_get_contents($viewStub));
}
}
}复制代码

结果

759bebac8f25da6766081a80298768e8.png

最后 使用

  • make:curd - 表示执行生成的命令

  • test - 为需要生成对应的controller+model+view+validate

  • --module - 需要在执行的模块中生成

php think make:curd test --module index
php think make:curd test --module admin
php think make:curd test

End

来源:https://juejin.cn/post/6877444191119474695

3d495d2dbab6422c910cb326a1f191fa.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值