介紹Laravel自訂Make指令產生Service類
下面由laravel教學欄位介紹Laravel自訂Make指令產生Service類,希望對需要的朋友有幫助!
環境說明
一樣我所使用的環境為:Laravel Framework 8.40.0
#為我所使用的環境為:
#。 C:\www\wwwroot\laravel8>php artisan --version
Laravel Framework 8.40.0
登入後複製
C:\www\wwwroot\laravel8>php artisan --version Laravel Framework 8.40.0
一、製作指令檔
- 前期知識的相關製作的教學課程,請參考我的另一篇部落格Laravel自訂Make指令產生目標類別。
-
執行以下指令
Console/Commands/MakeService.php<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"> php artisan make:command MakeService</pre><div class="contentsignin">登入後複製</div></div>
產生 指令檔。 -
getStub修改繼承類別
將繼承類別修改成GeneratorCommand
,該類別的命名空間為
Illuminate\Console\GeneratorCommand
。刪除實例化方法,handle函數
實作一個方法 。 -
name設定
name屬性。
修改$signature
屬性為 屬性,並設定指令: -
protected $name = 'make:service';
登入後複製
Service設定
type屬性值
type
類型設置,我們產生的是service
#,所以我們設定的屬性就是。
type類型是自己去定義的,本身沒有特殊意義,可以不用設定。protected $type = 'Service';
登入後複製type屬性值只是在創建錯誤的時候,給你一個友善的提示,如下所示:
type<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"> C:\www\wwwroot\laravel8>php artisan make:service TestService already exists! C:\www\wwwroot\laravel8>php artisan make:service TestService Service already exists!</pre><div class="contentsignin">登入後複製</div></div>
第一個是沒有設定type
#屬性的效果,第二個是設定了屬性的效果。
官方使用的type有:Controller,Middleware,Cast,Channel… 根據自己的需求修改其他的屬性 -
Services設定Stub的位置和指令空間
Stub的位置是在根目錄下
Stubs/service.stub裡面。
命名空間在app
目錄下 裡面。
實例程式碼如下:<?php
namespace App\Console\Commands;
use Illuminate\Console\GeneratorCommand;
class MakeService extends GeneratorCommand{
/**
* The console command name.
*
* @var string
*/
protected $name = 'make:service';
/**
* The console command description.
*
* @var string
*/
protected $description = '生成service对象类';
/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Service';
/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
// Implement getStub() method.
return $this->laravel->basePath('/stubs/service.stub');
}
/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Services';
}}
登入後複製
<?php namespace App\Console\Commands; use Illuminate\Console\GeneratorCommand; class MakeService extends GeneratorCommand{ /** * The console command name. * * @var string */ protected $name = 'make:service'; /** * The console command description. * * @var string */ protected $description = '生成service对象类'; /** * The type of class being generated. * * @var string */ protected $type = 'Service'; /** * Get the stub file for the generator. * * @return string */ protected function getStub() { // Implement getStub() method. return $this->laravel->basePath('/stubs/service.stub'); } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace($rootNamespace) { return $rootNamespace.'\Services'; }}
#二、製作Stub檔案
我的service檔案目前不需要繼承或依賴什麼類。所以,相對的比較簡單。如果你有特別的需要,可以進行擴充操作。
實例程式碼如下:類別內部會自動替換成自動產生的類別名稱和設定的命名空間。<?phpnamespace DummyNamespace;class DummyClass{ //}登入後複製DummyClass
和DummyNamespace
在繼承的GeneratorCommand
建議這種寫法,可以使用編輯器的語法提示,以獲得更友善的提示效果。
另外,你也可以使用Larave
內建的{{ class }}
和
寫法。
三、測試Service產生
執行以下指令
php artisan make:service IndexService
能正常產生成功 以上是介紹Laravel自訂Make指令產生Service類的詳細內容。更多資訊請關注PHP中文網其他相關文章!C:\www\wwwroot\laravel8>php artisan make:service IndexService
Service created successfully.
,產生的檔案如下:
###### #<?php
namespace App\Services;
class IndexService{
//}

熱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 是一款 PHP 框架,用於輕鬆構建 Web 應用程序。它提供一系列強大的功能,包括:安裝: 使用 Composer 全局安裝 Laravel CLI,並在項目目錄中創建應用程序。路由: 在 routes/web.php 中定義 URL 和處理函數之間的關係。視圖: 在 resources/views 中創建視圖以呈現應用程序的界面。數據庫集成: 提供與 MySQL 等數據庫的開箱即用集成,並使用遷移來創建和修改表。模型和控制器: 模型表示數據庫實體,控制器處理 HTTP 請求。

在使用CraftCMS開發網站時,常常會遇到資源文件緩存的問題,特別是當你頻繁更新CSS和JavaScript文件時,舊版本的文件可能仍然被瀏覽器緩存,導致用戶無法及時看到最新的更改。這個問題不僅影響用戶體驗,還會增加開發和調試的難度。最近,我在項目中遇到了類似的困擾,經過一番探索,我找到了wiejeben/craft-laravel-mix這個插件,它完美地解決了我的緩存問題。

Laravel 提供了一個全面的 Auth 框架,用於實現用戶登錄功能,包括:定義用戶模型(Eloquent 模型)創建登錄表單(Blade 模板引擎)編寫登錄控制器(繼承 Auth\LoginController)驗證登錄請求(Auth::attempt)登錄成功後重定向(redirect)考慮安全因素:哈希密碼、防 CSRF 保護、速率限制和安全標頭。此外,Auth 框架還提供重置密碼、註冊和驗證電子郵件等功能。詳情請參閱 Laravel 文檔:https://laravel.com/doc

想要學習 Laravel 框架,但苦於沒有資源或經濟壓力?本文為你提供了免費學習 Laravel 的途徑,教你如何利用網絡平台、文檔和社區論壇等資源,從入門到掌握,為你的 PHP 開發之旅奠定堅實基礎。

文章摘要:本文提供了詳細分步說明,指導讀者如何輕鬆安裝 Laravel 框架。 Laravel 是一個功能強大的 PHP 框架,它 упростил 和加快了 web 應用程序的開發過程。本教程涵蓋了從系統要求到配置數據庫和設置路由等各個方面的安裝過程。通過遵循這些步驟,讀者可以快速高效地為他們的 Laravel 項目打下堅實的基礎。

Laravel框架內置了多種方法來方便地查看其版本號,滿足開發者的不同需求。本文將探討這些方法,包括使用Composer命令行工具、訪問.env文件或通過PHP代碼獲取版本信息。這些方法對於維護和管理Laravel應用程序的版本控制至關重要。

在面向初学者的 Laravel 框架版本选择指南中,本文深入探討了 Laravel 的版本差異,旨在協助初學者在眾多版本之間做出明智的選擇。我們將重點介紹每個版本的關鍵特徵、比較它們的優缺點,並提供有用的建議,幫助新手根據他們的技能水準和項目需求挑選最合適的 Laravel 版本。對於初學者來說,選擇一個合適的 Laravel 版本至關重要,因為它可以顯著影響他們的學習曲線和整體開發體驗。

Laravel 和 ThinkPHP 都是流行的 PHP 框架,在開發中各有優缺點。本文將深入比較這兩者,重點介紹它們的架構、特性和性能差異,以幫助開發者根據其特定項目需求做出明智的選擇。
