這篇文章給大家分享的是有關(guān)Laravel中Facade怎么用的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
我們提供的服務(wù)有:成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、高淳ssl等。為1000+企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢(xún)和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的高淳網(wǎng)站制作公司
數(shù)據(jù)庫(kù)的使用:
$users = DB::connection('foo')->select(...);
眾所周知,IOC容器是 Laravel 框架的最最重要的部分。它提供了兩個(gè)功能,IOC和容器。
IOC(Inversion of Control),也叫控制反轉(zhuǎn)。說(shuō)白了,就是控制對(duì)象的生成,使開(kāi)發(fā)者不用關(guān)心對(duì)象的如何生成,只需要關(guān)心它的使用即可。
而通過(guò)IOC機(jī)制生成的對(duì)象實(shí)例需要一個(gè)存放的位置,以便之后繼續(xù)使用,便是它的容器功能。
這次不準(zhǔn)備講解IOC容器的具體實(shí)現(xiàn),之后會(huì)有文章詳細(xì)解讀它。關(guān)于IOC容器,讀者只需要記住兩點(diǎn)即可:
根據(jù)配置生成對(duì)象實(shí)例;
保存對(duì)象實(shí)例,方便隨時(shí)取用;
<?php namespace facades; abstract class Facade { protected static $app; /** * Set the application instance. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public static function setFacadeApplication($app) { static::$app = $app; } /** * Get the registered name of the component. * * @return string * * @throws \RuntimeException */ protected static function getFacadeAccessor() { throw new RuntimeException('Facade does not implement getFacadeAccessor method.'); } /** * Get the root object behind the facade. * * @return mixed */ public static function getFacadeRoot() { return static::resolveFacadeInstance(static::getFacadeAccessor()); } /** * Resolve the facade root instance from the container. * * @param string|object $name * @return mixed */ protected static function resolveFacadeInstance($name) { return static::$app->instances[$name]; } public static function __callStatic($method, $args) { $instance = static::getFacadeRoot(); if (! $instance) { throw new RuntimeException('A facade root has not been set.'); } switch (count($args)) { case 0: return $instance->$method(); case 1: return $instance->$method($args[0]); case 2: return $instance->$method($args[0], $args[1]); case 3: return $instance->$method($args[0], $args[1], $args[2]); case 4: return $instance->$method($args[0], $args[1], $args[2], $args[3]); default: return call_user_func_array([$instance, $method], $args); } } }
代碼說(shuō)明:
$app中存放的就是一個(gè)IOC容器實(shí)例,它是在框架初始化時(shí),通過(guò) setFacadeApplication() 這個(gè)靜態(tài)方法設(shè)置的
它實(shí)現(xiàn)了 __callStatic 魔術(shù)方法
getFacadeAccessor() 方法需要子類(lèi)去繼承,返回一個(gè)string的標(biāo)識(shí),通過(guò)這個(gè)標(biāo)識(shí),IOC容器便能返回它所綁定類(lèi)(框架初始化或其它時(shí)刻綁定)的對(duì)象
通過(guò) $instance 調(diào)用具體的方法
TEST1 的具體邏輯:
<?php class Test1{ public function hello() { print("hello world"); }}
TEST1 類(lèi)的Facade:
<?php namespace facades;/** * Class Test1 * @package facades * * @method static setOverRecommendInfo [設(shè)置播放完畢時(shí)的回調(diào)函數(shù)] * @method static setHandlerPlayer [明確指定下一首時(shí)的執(zhí)行類(lèi)] */class Test1Facade extends Facade{ protected static function getFacadeAccessor() { return 'test1'; } }
使用:
use facades\Test1Facade;Test1Facade::hello(); // 這是 Facade 調(diào)用
解釋?zhuān)?/p>
facades\Test1Facade 調(diào)用靜態(tài)方法 hello() 時(shí),由于沒(méi)有定義此方法,會(huì)調(diào)用 __callStatic;
在 __callStatic 中,首先是獲取對(duì)應(yīng)的實(shí)例,即 return static::$app->instances[$name];
。這其中的 $name
,即為 facades\Test1
里的 test1
$app, 即為 IOC 容器,類(lèi)的實(shí)例化過(guò)程,就交由它來(lái)處理。
感謝各位的閱讀!關(guān)于“Laravel中Facade怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
文章標(biāo)題:Laravel中Facade怎么用
轉(zhuǎn)載源于:http://www.rwnh.cn/article44/gjhphe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、網(wǎng)站排名、小程序開(kāi)發(fā)、網(wǎng)站制作、App設(shè)計(jì)、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)