Laravel如何自定义command命令浅析

2025-05-29 0 98

前言

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

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

自定义方法如下:

1.创建command类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50
<?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文件下创建自定义模板文件

?

1

2

3

4

5

6

7

8

9

10

11
<?php

namespace DummyNamespace;

class DummyClass

{

public function __construct()

{

}

}

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

运行测试

?

1
php artisan make:service Web/TestService

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

总结

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Laravel如何自定义command命令浅析 https://www.kuaiidc.com/93442.html

相关文章

猜你喜欢
发表评论
暂无评论