首页 > 编程语言 >Thinkphp6 自定义命令创建类文件

Thinkphp6 自定义命令创建类文件

时间:2023-04-12 15:35:32浏览次数:37  
标签:string 自定义 创建 make Thinkphp6 stub controller think

以创建控制器为例

1、先通过 think 命令创建一个make文件,效果如下图:

php think make:command make/MyController

 

2、修改上面创建的文件【MyController.php】

<?php
declare (strict_types = 1);

namespace app\command\make;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class MyController extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('mycontroller')
            ->setDescription('the mycontroller command');
    }

    protected function execute(Input $input, Output $output)
    {
        // 指令输出
        $output->writeln('mycontroller');
    }
}

/***************************************** 上面是原来的内容,下面是修改后的内容 *****************************************/

<?php
declare (strict_types = 1);

namespace app\command;


use think\console\command\Make;

/**
 * 自定义控制器模板
 * Class MyController
 * @package app\command
 */
class MyController extends Make
{
    protected $type = "MyController";

    protected function configure()
    {
        parent::configure();
        $this->setName('make:myController')
            ->setDescription('Create a new self controller class');
    }

    public function getStub(): string
    {
        $stubPath = __DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR;

        return $stubPath . 'controller.stub';
    }

    protected function getClassName(string $name): string
    {
        return parent::getClassName($name) . ($this->app->config->get('route.controller_suffix') ? 'Controller' : '');
    }

    protected function getNamespace(string $app): string
    {
        return parent::getNamespace($app);
    }
}

 

3、在 app/command/make 目录下创建名为【stubs】的目录,且在【stubs】下创建一个名为【controller.stub】的文件,效果如下图:

 

4、找到项目中原有的【controller.stub】文件,具体位置在【vendor/topthink/framework/src/think/console/command/make/stubs/controller.stub】。复制原有文件中的内容到新建的【controller.stub】中。

注意:【controller.stub】模板中的变量只有下图中的四个。

 

5、在【config/console.php】中加入第一步中创建的命令,效果如下图:

 

6、现在可以通过 think 的命令查看我们所创建的自定义命令,结果如下图:

php think

 

7、使用自定义的think命令创建一个名为【Test】控制器类,效果如下图:

//admin/controller/Test 这是文件的路径+文件名,根据自己项目调整
php think make:myController  admin/controller/Test

 

标签:string,自定义,创建,make,Thinkphp6,stub,controller,think
From: https://www.cnblogs.com/mklblog/p/17309816.html

相关文章

  • 二级指针创建二叉树节点与一级指针创建二叉树节点
     1、c++中的struct结构体变量定义可以直接“类型名变量名”,c中只能“struct类型名变量名”,可以通过typedef达到相同的效果;struct_x1{...}x1;是定义了类_x1和_x1的对象实例x1,typedefstruct_x2{...}x2; 定义了类_x2和_x2的类别名x2;typedefstruc......
  • sqlserver 使用脚本创建作业 (by me)
    usemastergo--定义创建作业DECLARE@jobiduniqueidentifier,@jobnamesysnameSET@jobname=N'testInterval'IFEXISTS(SELECT*FROMmsdb.dbo.sysjobsWHEREname=@jobname)EXECmsdb.dbo.sp_delete_job@[email protected]_add_job@job_......
  • 以阿里巴巴推荐的使用 ThreadPoolExecutor 构造函数自定义参数的方式来创建线程池
    importjava.util.concurrent.ArrayBlockingQueue;importjava.util.concurrent.ThreadPoolExecutor;importjava.util.concurrent.TimeUnit;publicclassThreadPoolExecutorDemo{privatestaticfinalintCORE_POOL_SIZE=5;privatestaticfinalintMAX......
  • 全网最详细中英文ChatGPT-GPT-4示例文档-智能评论创建从0到1快速入门——官网推荐的48
    目录Introduce简介setting设置Prompt提示Sampleresponse回复样本APIrequest接口请求python接口请求示例node.js接口请求示例curl命令示例json格式示例其它资料下载ChatGPT是目前最先进的AI聊天机器人,它能够理解图片和文字,生成流畅和有趣的回答。如果你想跟上AI时代的潮流......
  • 自定义Vo
    V0(valueobject)值对象通常用于业务层之间的数据传递,和PO-样也是仅仅包含数据而已。但应是抽象出的业务对象,可以和表对应,也可以不,这根据业务的需要。用new关键字创建由GC回收的。Viewobject:视图对象;接受页面传递来的数据,封装对象将业务处理完成的对象,封装成页面要用的数据/......
  • 自定义aop
    自定义aopAspectJ应该算的上是Java生态系统中最完整的AOP框架了。SpringAOP和AspectJAOP有什么区别?SpringAOP属于运行时增强,而AspectJ是编译时增强。SpringAOP基于代理(Proxying),而AspectJ基于字节码操作(BytecodeManipulation)。SpringAOP已经集成......
  • sqlalchemy快速插入数据,scoped_session线程安全,加载类上的装饰器,基本增删改查,django中
    今日内容sqlalchemy快速插入数据sqlalchemy是什么orm框架,跟其他web框架没有必然联系,可以独立使用安装,快速使用,执行原生sql创建表和删除表不能创建数据库不能修改字段(增加,删除)使用orm插入第一步:生成engine对象engine=create_engine("mysql+pymysql://[email protected].......
  • Winform-自定义按钮_播放图标
    usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Drawing.Drawing2D;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingSystem.Management.Inst......
  • 实现自定义接口以及嵌套类的应用
    interfaceCityNumber{//定义两个接口intgetNumber();}interfaceDestination{StringgetName();}publicclassMain{publicclassCitysimplementsCityNumber{//嵌套的内部类1privateintid=518;@OverridepublicintgetNum......
  • Linux创建定时删除日志任务
    1、例:脚本对应的要删除的目录为/home/logs在home目录创建文件clearLogFiles.sh:Linux下自动删除2小时之前的日志文件(或者清空日志)find/home/log/-mmin+120-name"*.log"-execrm-rf{}\;# >catalina.out2、为脚本增加可执行权限chmod+xclearLogFiles.sh3、编......