如何透過laravel來創建自訂artisan make的命令新建類文件
下面這篇文章主要給大家介紹了關於laravel如何透過建立自訂artisan make指令來新建類別檔案的相關資料,需要的朋友可以參考借鑒,下面來一起看看吧。
Laravel透過Artisan提供了強大的控制台命令來處理非瀏覽器業務邏輯。
前言
本文主要跟大家介紹的是關於laravel透過建立自訂artisan make指令來新建類別檔案的相關內容,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧。
我們在laravel開發時常用到artisan make:controller
等指令來新建Controller、Model、Job、Event等類別檔案。在Laravel5.2中artisan make
指令支援建立如下檔:
make:auth Scaffold basic login and registration views and routes make:console Create a new Artisan command make:controller Create a new controller class make:event Create a new event class make:job Create a new job class make:listener Create a new event listener class make:middleware Create a new middleware class make:migration Create a new migration file make:model Create a new Eloquent model class make:policy Create a new policy class make:provider Create a new service provider class make:request Create a new form request class make:seeder Create a new seeder class make:test Create a new test class
不過,有時候預設的並不能夠滿足我們的需求, 比方我們在專案中使用的Respository模式來進一步封裝了Model文件,就需要經常創建Repository類文件了,時間長了就會想能不能透過artisan make:repository
命令自動創建類文件而不是都每次手動建立。
系統自帶的artisan make
指令對應的PHP程式放在Illuminate\Foundation\Console目錄下,我們參考Illuminate\Foundation\Console\ProviderMakeCommand類別來定義自己的# artisan make:repository
指令。
一、建立命令類別
#在app\Console\Commands資料夾下建立RepositoryMakeCommand.php文件,具體程式如下:
namespace App\Console\Commands; use Illuminate\Console\GeneratorCommand; class RepositoryMakeCommand extends GeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'make:repository'; /** * The console command description. * * @var string */ protected $description = 'Create a new repository class'; /** * The type of class being generated. * * @var string */ protected $type = 'Repository'; /** * Get the stub file for the generator. * * @return string */ protected function getStub() { return __DIR__.'/stubs/repository.stub'; } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace($rootNamespace) { return $rootNamespace.'\Repositories'; } }
namespace DummyNamespace; use App\Repositories\BaseRepository; class DummyClass extends BaseRepository { /** * Specify Model class name * * @return string */ public function model() { //set model name in here, this is necessary! } }
protected $commands = [ Commands\RepositoryMakeCommand::class ];
php artisan make:repository TestRepository php artisan make:repository SubDirectory/TestRepository
以上是如何透過laravel來創建自訂artisan make的命令新建類文件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Laravel郵件發送失敗時的退信代碼獲取方法在使用Laravel開發應用時,經常會遇到需要發送驗證碼的情況。而在實�...

在dcatadmin(laravel-admin)中如何實現自定義點擊添加數據的表格功能在使用dcat...

Laravel框架中Redis連接的共享與select方法的影響在使用Laravel框架和Redis時,開發者可能會遇到一個問題:通過配置...

在Laravel多租戶擴展包stancl/tenancy中自定義租戶數據庫連接使用Laravel多租戶擴展包stancl/tenancy構建多租戶應用時,...

LaravelEloquent模型檢索:輕鬆獲取數據庫數據EloquentORM提供了簡潔易懂的方式來操作數據庫。本文將詳細介紹各種Eloquent模型檢索技巧,助您高效地從數據庫中獲取數據。 1.獲取所有記錄使用all()方法可以獲取數據庫表中的所有記錄:useApp\Models\Post;$posts=Post::all();這將返回一個集合(Collection)。您可以使用foreach循環或其他集合方法訪問數據:foreach($postsas$post){echo$post->

在Laravel6項目中如何檢查Redis連接的有效性是一個常見的問題,特別是在項目依賴於Redis進行業務處理時。以下是...

Laravel數據庫遷移過程中出現類重複定義問題在使用Laravel框架進行數據庫遷移時,開發者可能會遇到“類已使用�...
