• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    Laravel如何自定义command命令浅析

    前言

    用过Laravel的都知道,Laravel通过php artisan make:controller可以生成控制器,同样的夜可以用命令生成中间介和模型,那怎么自定义生成文件呢?

    下面话不多说了,来一起看看详细的介绍吧

    自定义方法如下:

    1.创建command类

    ?php
    
    namespace App\Console\Commands;
    
    use Illuminate\Console\GeneratorCommand;
    
    class ServiceMakeCommand extends GeneratorCommand
    {
     /**
      * The console command name.
      *
      * @var string
      */
     protected $name = 'make:service';
    
     /**
      * The console command description.
      *
      * @var string
      */
     protected $description = 'Create a new service class';
    
     /**
      * The type of class being generated.
      *
      * @var string
      */
     protected $type = 'Services';
    
     /**
      * Get the stub file for the generator.
      *
      * @return string
      */
     protected function getStub()
     {
      return __DIR__.'/stubs/service.stub';
     }
    
     /**
      * Get the default namespace for the class.
      *
      * @param string $rootNamespace
      * @return string
      */
     protected function getDefaultNamespace($rootNamespace)
     {
      return $rootNamespace."\Services";
     }
    }

    2.在Commands/stubs文件下创建自定义模板文件

    ?php
    
    namespace DummyNamespace;
    
    class DummyClass 
    {
     public function __construct()
     {
    
     }
    }

    创建了一个只有构造函数的类,具体模板可以自己定义

    运行测试

    php artisan make:service Web/TestService

    这个时候Services文件下的Web目录下会生成TestService文件,Web目录不存在时会自动创建

    总结

    以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。

    您可能感兴趣的文章:
    • Laravel学习笔记之Artisan命令生成自定义模板的方法
    • 详细Laravel5.5执行表迁移命令出现表为空的解决方案
    • laravel通过创建自定义artisan make命令来新建类文件详解
    • laravel 创建命令行命令的图文教程
    上一篇:PHP使用mysqli同时执行多条sql查询语句的实例
    下一篇:PHP使用OB缓存实现静态化功能示例
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    Laravel如何自定义command命令浅析 Laravel,如何,自定义,command,