artisan 命令1

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

作者:VEPHP   时间 2017-09-08

《PHP实例:laravel通过创建自定义artisan make命令来新建类文件详解》要点:
本文介绍了PHP实例:laravel通过创建自定义artisan make命令来新建类文件详解,希望对您有用。如果有疑问,可以联系我们。

PHP实例前言

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

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


  
  1. PHP实例
  2. make:auth Scaffold basic login and registration views and routes
  3. make:console Create a new Artisan command
  4. make:controller Create a new controller class
  5. make:event Create a new event class
  6. make:job Create a new job class
  7. make:listener Create a new event listener class
  8. make:middleware Create a new middleware class
  9. make:migration Create a new migration file
  10. make:model Create a new Eloquent model class
  11. make:policy Create a new policy class
  12. make:provider Create a new service provider class
  13. make:request Create a new form request class
  14. make:seeder Create a new seeder class
  15. make:test Create a new test class

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

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

PHP实例一、创建命令类

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


  
  1. PHP实例
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\GeneratorCommand;
  4. class RepositoryMakeCommand extends GeneratorCommand
  5. {
  6. /**
  7. * The console command name.
  8. *
  9. * @var string
  10. */
  11. protected $name = 'make:repository';
  12. /**
  13. * The console command description.
  14. *
  15. * @var string
  16. */
  17. protected $description = 'Create a new repository class';
  18. /**
  19. * The type of class being generated.
  20. *
  21. * @var string
  22. */
  23. protected $type = 'Repository';
  24. /**
  25. * Get the stub file for the generator.
  26. *
  27. * @return string
  28. */
  29. protected function getStub()
  30. {
  31. return __DIR__.'/stubs/repository.stub';
  32. }
  33. /**
  34. * Get the default namespace for the class.
  35. *
  36. * @param string $rootNamespace
  37. * @return string
  38. */
  39. protected function getDefaultNamespace($rootNamespace)
  40. {
  41. return $rootNamespace.'\Repositories';
  42. }
  43. }

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

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


  
  1. PHP实例
  2. namespace DummyNamespace;
  3. use App\Repositories\BaseRepository;
  4. class DummyClass extends BaseRepository
  5. {
  6. /**
  7. * Specify Model class name
  8. *
  9. * @return string
  10. */
  11. public function model()
  12. {
  13. //set model name in here, this is necessary!
  14. }
  15. }

PHP实例三、注册命令类

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


  
  1. PHP实例
  2. protected $commands = [
  3. Commands\RepositoryMakeCommand::class
  4. ];

PHP实例测试命令

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


  
  1. PHP实例
  2. php artisan make:repository TestRepository
  3. php artisan make:repository SubDirectory/TestRepository

PHP实例总结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值