前言
用過Laravel的都知道,Laravel通過php artisan make:controller可以生成控制器,同樣的夜可以用命令生成中間介和模型,那怎么自定義生成文件呢?
下面話不多說了,來一起看看詳細(xì)的介紹吧
自定義方法如下:
1.創(chuàng)建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文件下創(chuàng)建自定義模板文件
1
2
3
4
5
6
7
8
9
10
11
|
<?php namespace DummyNamespace; class DummyClass { public function __construct() { } } |
創(chuàng)建了一個(gè)只有構(gòu)造函數(shù)的類,具體模板可以自己定義
運(yùn)行測試
1
|
php artisan make:service Web/TestService |
這個(gè)時(shí)候Services文件下的Web目錄下會(huì)生成TestService文件,Web目錄不存在時(shí)會(huì)自動(dòng)創(chuàng)建
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對服務(wù)器之家的支持。