laravel通过创建自定义artisan make命令来新建类文件详解

2025-05-27 0 104

前言

本文主要跟大家介绍的是关于laravel通过创建自定义artisan make命令来新建类文件的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

我们在laravel开发时经常用到artisan make:controller等命令来新建Controller、Model、Job、Event等类文件。 在Laravel5.2中artisan make命令支持创建如下文件:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
make:auth Scaffold basic login and registration views and routes

make:console Create a new Artisan command

make:controller Create a new controller class

make:event Create a new event class

make:job Create a new job class

make:listener Create a new event listener class

make:middleware Create a new middleware class

make:migration Create a new migration file

make:model Create a new Eloquent model class

make:policy Create a new policy class

make:provider Create a new service provider class

make:request Create a new form request class

make:seeder Create a new seeder class

make:test Create a new test class

不过,有时候默认的并不能够满足我们的需求, 比方我们在项目中使用的Respository模式来进一步封装了Model文件,就需要经常创建Repository类文件了,时间长了就会想能不能通过artisan make:repository命令自动创建类文件而不是都每次手动创建。

系统自带的artisan make命令对应的PHP程序放在Illuminate\\Foundation\\Console目录下,我们参照Illuminate\\Foundation\\Console\\ProviderMakeCommand类来定义自己的artisan make:repository命令。

一、创建命令类

在app\\Console\\Commands文件夹下创建RepositoryMakeCommand.php文件,具体程序如下:

?

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
namespace App\\Console\\Commands;

use Illuminate\\Console\\GeneratorCommand;

class RepositoryMakeCommand extends GeneratorCommand

{

/**

* The console command name.

*

* @var string

*/

protected $name = 'make:repository';

/**

* The console command description.

*

* @var string

*/

protected $description = 'Create a new repository class';

/**

* The type of class being generated.

*

* @var string

*/

protected $type = 'Repository';

/**

* Get the stub file for the generator.

*

* @return string

*/

protected function getStub()

{

return __DIR__.'/stubs/repository.stub';

}

/**

* Get the default namespace for the class.

*

* @param string $rootNamespace

* @return string

*/

protected function getDefaultNamespace($rootNamespace)

{

return $rootNamespace.'\\Repositories';

}

}

二、创建命令类对应的模版文件

在app\\Console\\Commands\\stubs下创建模版文件 .stub文件是make命令生成的类文件的模版,用来定义要生成的类文件的通用部分创建repository.stub模版文件:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17
namespace DummyNamespace;

use App\\Repositories\\BaseRepository;

class DummyClass extends BaseRepository

{

/**

* Specify Model class name

*

* @return string

*/

public function model()

{

//set model name in here, this is necessary!

}

}

三、注册命令类

将RepositoryMakeCommand添加到App\\Console\\Kernel.php中

?

1

2

3
protected $commands = [

Commands\\RepositoryMakeCommand::class

];

测试命令

好了, 现在就可以通过make:repository命令来创建repository类文件了

?

1

2

3
php artisan make:repository TestRepository

php artisan make:repository SubDirectory/TestRepository

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对快网idc的支持。

原文链接:https://segmentfault.com/a/1190000010698217

收藏 (0) 打赏

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

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

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

快网idc优惠网 建站教程 laravel通过创建自定义artisan make命令来新建类文件详解 https://www.kuaiidc.com/72048.html

相关文章

发表评论
暂无评论