因:项目复杂,创建多个模型每次都写软删除等操作,故使用tp自定义指令避免重复操作
tips : addArgument和addOptional区别在于 argument的参数是必须写的 php think hello World。此时hello是命令名称world是必传参数。如果是optional,那么命令则为php think hello World -g “hush” 则输出helloworld hush。不写-g和后面的参数直接执行php think hello World也不会报错。
- 1.php think make:command Hush hush 第一个Hush是文件名称,生成之后位于app\command。第二个hush则是命令名称,是php think hush 的那个hush

- 2.config/console.php 把生成的类添加进去

- 3. 命令内容–自己在app目录下创建一个baseModel 内容自定
protected function configure()
{
// 指令配置
$this->setName('hush')
->setDescription('继承自定义模型基础类')
->addArgument('name', Argument::REQUIRED, 'The name of the model');
}
protected function execute(Input $input, Output $output)
{
$name = $input->getArgument('name');
$modelNamespace = 'app\\model';
// 模型文件路径
$filePath = $this->app->getRootPath() . "app/model/{$name}.php";
if (file_exists($filePath)) {
$output->writeln("<error>Model {$name} already exists!</error>");
return;
}
// 模型类内容
$modelContent = <<<EOD
<?php
namespace $modelNamespace;
use app\BaseModel;
class $name extends BaseModel
{
// 写自己的模型基本操作 软删除等
}
EOD;
// 写入模型文件
file_put_contents($filePath, $modelContent);
$output->writeln("<info>Model {$name} created successfully.</info>");
}
