前言
使用Thinkphp框架开发项目时,将常量统一定义到一个模块的类文件中是很有必要的,方便我们开发和维护。但是每次都要手动创建文件比较麻烦,写了一个命令行生成常量的类文件的扩展包,可以像创建控制器和模型一样使用命令行创建。
github地址(求个start⭐):jian1098/tp-constant: thinkphp框架命令行创建常量class文件 (github.com)
如何使用
-
安装
thinkphp5.*安装
1.0.1
版本composer require jian1098/tp-constant:1.0.1
然后在
application/command.php
新增一行注册服务return [ 'Jian1098\TpConstant\Command\Make\Constant' ];
thinkphp6以上安装
2.0.1
版本,不需要手动注册服务composer require jian1098/tp-constant:2.0.1
安装完成后在控制台输入
php think
可以看到多了make:constant
命令make make:command Create a new command class make:constant Create a new constant class # 新增的命令 make:controller Create a new resource controller class make:middleware Create a new middleware class make:model Create a new model class
-
使用命令行创建常量class
php think make:constant TestConstant
该命令会创建
application/common/constant/TestConstant.php
文件 -
调用常量
<?php namespace app\api\controller; use app\common\constant\TestConstant; use think\Controller; class Test extends Controller { //index public function index() { var_dump(TestConstant::SERVER_ERROR); //使用常量 } }
其他好用的扩展
jian1098/tp5-validate: thinkphp5.0框架命令行创建验证类validate (github.com)
配合使用开发更方便哦