Laravel如何自定义command命令浅析

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Laravel如何自定义command命令浅析脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

前言

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

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

自定义方法如下:

1.创建command类

<&#63;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如何自定义command命令浅析全部内容,希望文章能够帮你解决Laravel如何自定义command命令浅析所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。