Laravel如何自定義command命令淺析
前言
用過Laravel的都知道,Laravel通過php artisan make:controller可以生成控制器,同樣的夜可以用命令生成中間介和模型,那怎么自定義生成文件呢?
下面話不多說了,來一起看看詳細的介紹吧
自定義方法如下:
1.創(chuàng)建command類
<?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)建自定義模板文件
<?php
namespace DummyNamespace;
class DummyClass
{
public function __construct()
{
}
}
創(chuàng)建了一個只有構(gòu)造函數(shù)的類,具體模板可以自己定義
運行測試
php artisan make:service Web/TestService
這個時候Services文件下的Web目錄下會生成TestService文件,Web目錄不存在時會自動創(chuàng)建
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。
相關文章
discuz 首頁四格:最新話題+最新回復+熱門話題+精華文章插件
discuz 首頁四格:最新話題+最新回復+熱門話題+精華文章插件...2007-08-08
laravel使用組件實現(xiàn)微信網(wǎng)頁授權登入
這篇文章主要介紹了laravel使用組件實現(xiàn)微信網(wǎng)頁授權登入,使用laravel組件 laravel-wechat調(diào)用,使用起來很方便,有需要的同學可以學習下2021-03-03
PHP實現(xiàn)PDF轉(zhuǎn)圖片的詳細過程(使用imagick)
最近有一份pdf文件,需要將其轉(zhuǎn)換成圖片,所以這篇文章主要給大家介紹了關于PHP實現(xiàn)PDF轉(zhuǎn)圖片的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2023-01-01

