今天就跟大家聊聊有關怎么在Laravel中自定命令并生成文件,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
以創建service層為例子
1、執行命令
php artisan make:command ServiceMakeCommand
2、在app\Console\Commands 下就會多出一個 ServiceMakeCommand.php 文件 ,更改其內容為一下內容 ( 注意:
1、承了GeneratorCommand類,
2、protected $signature = 'make:service {name}'; 中{name}必須要有
<?php namespace App\Console\Commands; use Illuminate\Console\GeneratorCommand; class ServiceMakeCommand extends GeneratorCommand { /** * The name and signature of the console command. * * @var string */ protected $signature = 'make:service {name}'; /** * The console command description. * * @var string */ protected $description = 'Create a service'; /** * 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'; } }
3、創建模版
在 app\Console\Commands\ 下創建stubs文件夾 ,并創建文件service.stub,其內容為
<?php namespace DummyNamespace; class DummyClass { public function __construct() { parent::__construct(); } }
4、現在就已經完成了,運行 php artisan list,就可以看到
執行 php artisan make:service BaseService 就有BaseService.php 文件了
Laravel 是一套簡潔、優雅的PHP Web開發框架。它可以讓你從面條一樣雜亂的代碼中解脫出來;它可以幫你構建一個完美的網絡APP,而且每行代碼都可以簡潔、富于表達力。
看完上述內容,你們對怎么在Laravel中自定命令并生成文件有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。