溫馨提示×

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

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

怎么在Laravel中自定命令并生成文件

發(fā)布時(shí)間:2021-05-17 15:54:04 來源:億速云 閱讀:191 作者:Leah 欄目:開發(fā)技術(shù)

今天就跟大家聊聊有關(guān)怎么在Laravel中自定命令并生成文件,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

以創(chuàng)建service層為例子

1、執(zhí)行命令

php artisan make:command ServiceMakeCommand

2、在app\Console\Commands 下就會(huì)多出一個(gè) ServiceMakeCommand.php 文件 ,更改其內(nèi)容為一下內(nèi)容 ( 注意:

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、創(chuàng)建模版

在 app\Console\Commands\ 下創(chuàng)建stubs文件夾 ,并創(chuàng)建文件service.stub,其內(nèi)容為

<?php

namespace DummyNamespace;

class DummyClass
{
 public function __construct()
 {
 parent::__construct();
 }
}

4、現(xiàn)在就已經(jīng)完成了,運(yùn)行 php artisan list,就可以看到

怎么在Laravel中自定命令并生成文件

執(zhí)行 php artisan make:service BaseService 就有BaseService.php 文件了

怎么在Laravel中自定命令并生成文件

Laravel 是什么

Laravel 是一套簡潔、優(yōu)雅的PHP Web開發(fā)框架。它可以讓你從面條一樣雜亂的代碼中解脫出來;它可以幫你構(gòu)建一個(gè)完美的網(wǎng)絡(luò)APP,而且每行代碼都可以簡潔、富于表達(dá)力。

看完上述內(nèi)容,你們對(duì)怎么在Laravel中自定命令并生成文件有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

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

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

AI