溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Laravel怎樣自定義command命令淺析

發(fā)布時(shí)間:2021-02-04 10:35:09 來(lái)源:億速云 閱讀:304 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)Laravel怎樣自定義command命令淺析的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

前言

用過(guò)Laravel的都知道,Laravel通過(guò)php artisan make:controller可以生成控制器,同樣的夜可以用命令生成中間介和模型,那怎么自定義生成文件呢?

下面話(huà)不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧

自定義方法如下:

1.創(chuàng)建command類(lèi)

<?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è)只有構(gòu)造函數(shù)的類(lèi),具體模板可以自己定義

運(yùn)行測(cè)試

php artisan make:service Web/TestService

這個(gè)時(shí)候Services文件下的Web目錄下會(huì)生成TestService文件,Web目錄不存在時(shí)會(huì)自動(dòng)創(chuàng)建

感謝各位的閱讀!關(guān)于“Laravel怎樣自定義command命令淺析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI