当前位置: 移动技术网 > IT编程>开发语言>PHP > Laravel如何自定义command命令浅析

Laravel如何自定义command命令浅析

2019年07月19日  | 移动技术网IT编程  | 我要评论

外国房网,免费穿越小说,小调电影网

前言

用过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目录不存在时会自动创建

总结

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网