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

2,136 阅读1分钟

Chapter 1

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

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

生成命令行文件

php think make:command Curd curd

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

php think curd

Chapter 2

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

controller 内容

<?php
/**
 * Created by.
 * User: Jim
 * Date: 2020/9/28
 * Time: 14:31
 */

namespace app\$module\controller;


use think\Controller;

class $controller extends Controller
{

}

model 文件内容

<?php
/**
 * Created by.
 * User: Jim
 * Date: 2020/9/28
 * Time: 14:31
 */

namespace app\$module\model;


use think\Model;

class $model extends Model
{

}

Validate文件内容

<?php
/**
 * Created by.
 * User: Jim
 * Date: 2020/9/28
 * Time: 14:48
 */

namespace app\$module\validate;


use think\Validate;

class $validate extends Validate
{

}

View 待定·····

····

Chapter 3

<?php
/**
 * Created by.
 * User: Jim
 * 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));
        }

    }
}

结果

最后 使用

  • 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