在laravel中,make方法用來從容器當中解析一個type,該type是源碼當中定義的,解析後傳回的結果就是type的一個實例,容器類別呼叫make方法時,若沒有已註冊的key,會自動透過反射類別實例化具體類別。
本文操作環境:Windows10系統、Laravel6版、Dell G3電腦。
laravel中make方法的作用是什麼
Laravel中的make方法是用來從容器當中解析一個type,這個type是源碼當中定義的,不是很好翻譯成中文。解析後回傳的結果就是type的一個實例。
看過原始碼的同學應該知道在Illuminate\Foundation\Application這個類別和它的父類別Illuminate\Container\Container類別中都有make方法,那麼當執行如index.php中的這行程式碼,
1 $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
的時候,就會先去執行Illuminate\Foundation\Application中的make方法,那我們就先看看它。 (這篇文章就以make這個Kernel類別為例)
/** * Resolve the given type from the container. 从容器当中解析给定的type * * (Overriding Container::make) 覆盖了父类中的make方法 * * @param string $abstract 给定的type * @param array $parameters 指定一些参数 可选项 * @return mixed */ public function make($abstract, array $parameters = []) { $abstract = $this->getAlias($abstract);//调用父类中的getAlias方法 //如果在deferredServices这个数组设置了这个type并且在instances数组中没有设置这个type if (isset($this->deferredServices[$abstract]) && ! isset($this->instances[$abstract])) { $this->loadDeferredProvider($abstract);//那么就执行这个方法:加载被定义为延迟的服务提供者 } return parent::make($abstract, $parameters);//调用父类的make方法 }
好,我們一步一步的來,先看看這個getAlias方法,這個方法的作用就是傳回這個類別的別名,如果給出的是一個完整的類別名稱且在aliases中已經設定了那麼就返回這個類別名的別名,如果沒有設定過就返回這個類別名稱本身,大家在看這個方法的時候可以先var_dump一下$app,對照著看裡面的aliases數組,框架作者寫這個方法真的很巧妙,至少這種遞歸方式在我實際開發當中很少用到。
/** * Get the alias for an abstract if available. * * @param string $abstract * @return string * * @throws \LogicException */ public function getAlias($abstract) { if (! isset($this->aliases[$abstract])) { return $abstract; } if ($this->aliases[$abstract] === $abstract) { throw new LogicException("[{$abstract}] is aliased to itself."); } return $this->getAlias($this->aliases[$abstract]); }
接下來就是對deferredServices和instances這個兩個陣列進行判斷,在本例$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class); 當中,判斷的結果為false,因此不執行loadDeferredProvider方法。
再接下來就是呼叫父類別Illuminate\Container\Container中的make方法了,
/** * Resolve the given type from the container. * * @param string $abstract * @param array $parameters * @return mixed */ public function make($abstract, array $parameters = []) { return $this->resolve($abstract, $parameters);//直接调用resolve方法 }
【相關推薦:laravel影片教學】
以上是laravel中make法的作用是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

遷移在laravelmanagedatabaseschema,同時shandledatainterAction.1)遷移術語,允許創造,修改和deletionoftables

SoftDeletsinlaraveRareBetterTernaverainteraldatialdataAndRecoverability,而骨質骨骼驗證了forderableford.fordableablefordataminimization和Privacy.1)softerdeleteseThesoftDeletDeletEstrait,允許restrestoratorralityandaudtrails和mayincroredatabasesize.2)物理

softDeletsinlesInLaravelAlavareAfeAtarowSyOutOmarkRecordsAsdeletedwithOutreMovingThemfromthedataBase.toimplementsoftsoftdeletes:1)addtheSoftDeletDeleteStraittRaittRaiteRemodoyouRmodeNClandInclandInclandEncludEthedeletdelet_atcolumted_atcolumn.2)

laravelmigrationsareefectectivectiveDueTotheirversionControlandRoranderibalsible,slepliningDatabasemagementInwebDevelopment.1)heSpapsulatesCheMachangeNphpClasses,允許easyerollbacks

LaravelMigrationsareArareBestWhenFollowingTheSepractices:1)用戶清除,描述性formigrations,例如'addemailtouserstable'.2)ensuremigrationsareReereSareReverSiblewitha'down'method.3)考慮到the the the the the the the the the the the the the the broaderimptactondataintegnegrityAndegrityAndegrinegrityAndertality.4)optimizeperformanceb

使用Laravel和Vue.js可以構建單頁面應用(SPA)。 1)在Laravel中定義API路由和控制器,處理數據邏輯。 2)在Vue.js中創建組件化前端,實現用戶界面和數據交互。 3)配置CORS和使用axios進行數據交互。 4)利用VueRouter實現路由管理,提升用戶體驗。

在Laravel中創建自定義輔助函數的步驟是:1.在composer.json中添加自動加載配置;2.運行composerdump-autoload更新自動加載器;3.在app/Helpers目錄下創建並定義函數。這些函數能簡化代碼,提高可讀性和可維護性,但需注意命名衝突和測試性。

在Laravel中處理數據庫事務時,應使用DB::transaction方法,並註意以下要點:1.使用lockForUpdate()鎖定記錄;2.通過try-catch塊處理異常,並在需要時手動回滾或提交事務;3.考慮事務的性能,縮短執行時間;4.避免死鎖,可使用attempts參數重試事務。這段摘要完整地概括瞭如何在Laravel中優雅地處理事務,並提煉了文章中的核心要點和最佳實踐。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

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

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