您好,登錄后才能下訂單哦!
這篇文章主要介紹Laravel中Contracts和Facades有什么用,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
Contracts
Contracts其實(shí)就是倡導(dǎo)面向接口編程,來(lái)達(dá)到解耦的目的。而這些通用的接口已經(jīng)由Laravel為你設(shè)計(jì)好了。就是這些Contracts.
那么Laravel如何知道我們需要使用哪個(gè)實(shí)現(xiàn)呢?
在Laravel默認(rèn)的Contracts綁定中,在'Illuminate/Foundation/Application.php'有這樣的定義:這就是綁定了默認(rèn)的接口實(shí)現(xiàn).
/** * Register the core class aliases in the container. * * @return void */ public function registerCoreContainerAliases() { $aliases = [ 'app' => ['Illuminate\Foundation\Application', 'Illuminate\Contracts\Container\Container', 'Illuminate\Contracts\Foundation\Application'], 'auth' => 'Illuminate\Auth\AuthManager', 'auth.driver' => ['Illuminate\Auth\Guard', 'Illuminate\Contracts\Auth\Guard'], 'auth.password.tokens' => 'Illuminate\Auth\Passwords\TokenRepositoryInterface', 'blade.compiler' => 'Illuminate\View\Compilers\BladeCompiler', 'cache' => ['Illuminate\Cache\CacheManager', 'Illuminate\Contracts\Cache\Factory'], 'cache.store' => ['Illuminate\Cache\Repository', 'Illuminate\Contracts\Cache\Repository'], 'config' => ['Illuminate\Config\Repository', 'Illuminate\Contracts\Config\Repository'], 'cookie' => ['Illuminate\Cookie\CookieJar', 'Illuminate\Contracts\Cookie\Factory', 'Illuminate\Contracts\Cookie\QueueingFactory'], 'encrypter' => ['Illuminate\Encryption\Encrypter', 'Illuminate\Contracts\Encryption\Encrypter'], 'db' => 'Illuminate\Database\DatabaseManager', 'db.connection' => ['Illuminate\Database\Connection', 'Illuminate\Database\ConnectionInterface'], 'events' => ['Illuminate\Events\Dispatcher', 'Illuminate\Contracts\Events\Dispatcher'], 'files' => 'Illuminate\Filesystem\Filesystem', 'filesystem' => ['Illuminate\Filesystem\FilesystemManager', 'Illuminate\Contracts\Filesystem\Factory'], 'filesystem.disk' => 'Illuminate\Contracts\Filesystem\Filesystem', 'filesystem.cloud' => 'Illuminate\Contracts\Filesystem\Cloud', 'hash' => 'Illuminate\Contracts\Hashing\Hasher', 'translator' => ['Illuminate\Translation\Translator', 'Symfony\Component\Translation\TranslatorInterface'], 'log' => ['Illuminate\Log\Writer', 'Illuminate\Contracts\Logging\Log', 'Psr\Log\LoggerInterface'], 'mailer' => ['Illuminate\Mail\Mailer', 'Illuminate\Contracts\Mail\Mailer', 'Illuminate\Contracts\Mail\MailQueue'], 'auth.password' => ['Illuminate\Auth\Passwords\PasswordBroker', 'Illuminate\Contracts\Auth\PasswordBroker'], 'queue' => ['Illuminate\Queue\QueueManager', 'Illuminate\Contracts\Queue\Factory', 'Illuminate\Contracts\Queue\Monitor'], 'queue.connection' => 'Illuminate\Contracts\Queue\Queue', 'redirect' => 'Illuminate\Routing\Redirector', 'redis' => ['Illuminate\Redis\Database', 'Illuminate\Contracts\Redis\Database'], 'request' => 'Illuminate\Http\Request', 'router' => ['Illuminate\Routing\Router', 'Illuminate\Contracts\Routing\Registrar'], 'session' => 'Illuminate\Session\SessionManager', 'session.store' => ['Illuminate\Session\Store', 'Symfony\Component\HttpFoundation\Session\SessionInterface'], 'url' => ['Illuminate\Routing\UrlGenerator', 'Illuminate\Contracts\Routing\UrlGenerator'], 'validator' => ['Illuminate\Validation\Factory', 'Illuminate\Contracts\Validation\Factory'], 'view' => ['Illuminate\View\Factory', 'Illuminate\Contracts\View\Factory'], ];
在我們自定義的接口實(shí)現(xiàn)時(shí),我們可以在ServiceProvider中使用進(jìn)行綁定:
$this->app->bind('App\Contracts\EventPusher', 'App\Services\PusherEventPusher');
Facades
Facades 為應(yīng)用程序的服務(wù)容器中可用的類(lèi)提供了一個(gè)「靜態(tài)」接口。Laravel 「facades」作為在服務(wù)容器內(nèi)基類(lèi)的「靜態(tài)代理」。很難懂?
我們打開(kāi)項(xiàng)目目錄下的config/app.php,然后找到
/* |-------------------------------------------------------------------------- | Class Aliases |-------------------------------------------------------------------------- | | This array of class aliases will be registered when this application | is started. However, feel free to register as many as you wish as | the aliases are "lazy" loaded so they don't hinder performance. | */ 'aliases' => [ 'App' => Illuminate\Support\Facades\App::class, 'Artisan' => Illuminate\Support\Facades\Artisan::class, 'Auth' => Illuminate\Support\Facades\Auth::class, 'Blade' => Illuminate\Support\Facades\Blade::class, 'Bus' => Illuminate\Support\Facades\Bus::class, 'Cache' => Illuminate\Support\Facades\Cache::class, 'Config' => Illuminate\Support\Facades\Config::class, 'Cookie' => Illuminate\Support\Facades\Cookie::class, 'Crypt' => Illuminate\Support\Facades\Crypt::class, 'DB' => Illuminate\Support\Facades\DB::class, 'Eloquent' => Illuminate\Database\Eloquent\Model::class, 'Event' => Illuminate\Support\Facades\Event::class, 'File' => Illuminate\Support\Facades\File::class, 'Gate' => Illuminate\Support\Facades\Gate::class, 'Hash' => Illuminate\Support\Facades\Hash::class, 'Input' => Illuminate\Support\Facades\Input::class, 'Lang' => Illuminate\Support\Facades\Lang::class, 'Log' => Illuminate\Support\Facades\Log::class, 'Mail' => Illuminate\Support\Facades\Mail::class, 'Password' => Illuminate\Support\Facades\Password::class, 'Queue' => Illuminate\Support\Facades\Queue::class, 'Redirect' => Illuminate\Support\Facades\Redirect::class, 'Redis' => Illuminate\Support\Facades\Redis::class, 'Request' => Illuminate\Support\Facades\Request::class, 'Response' => Illuminate\Support\Facades\Response::class, 'Route' => Illuminate\Support\Facades\Route::class, 'Schema' => Illuminate\Support\Facades\Schema::class, 'Session' => Illuminate\Support\Facades\Session::class, 'Storage' => Illuminate\Support\Facades\Storage::class, 'URL' => Illuminate\Support\Facades\URL::class, 'Validator' => Illuminate\Support\Facades\Validator::class, 'View' => Illuminate\Support\Facades\View::class, ],
你是不是發(fā)現(xiàn)了什么?對(duì),F(xiàn)acades其實(shí)就是在config/app.php中定義的一系列類(lèi)的別名。只不過(guò)這些類(lèi)都具有一個(gè)共同的特點(diǎn),那就是繼承基底 Illuminate\Support\Facades\Facade 類(lèi)并實(shí)現(xiàn)一個(gè)方法:getFacadeAccessor返回名稱(chēng)。
自定義Facade
參考http://www.tutorialspoint.com/laravel/laravel_facades.htm
Step 1 ?創(chuàng)建一個(gè)名為 TestFacadesServiceProvider的ServiceProvider ,使用如下命令即可:
php artisan make:provider TestFacadesServiceProvider
Step 2 ? 創(chuàng)建一個(gè)底層代理類(lèi),命名為“TestFacades.php” at “App/Test”.
App/Test/TestFacades.php
<?php namespace App\Test; class TestFacades{ public function testingFacades(){ echo "Testing the Facades in Laravel."; } } ?>
Step 3 ? 創(chuàng)建一個(gè) Facade 類(lèi) called “TestFacades.php” at “App/Test/Facades”.
App/Test/Facades/TestFacades.php
<?php namespace app\Test\Facades; use Illuminate\Support\Facades\Facade; class TestFacades extends Facade{ protected static function getFacadeAccessor() { return 'test'; } }
Step 4 ?創(chuàng)建一個(gè)ServiceProviders類(lèi),名為“TestFacadesServiceProviders.php” at “App/Test/Facades”.
App/Providers/TestFacadesServiceProviders.php
<?php namespace App\Providers; use App; use Illuminate\Support\ServiceProvider; class TestFacadesServiceProvider extends ServiceProvider { public function boot() { // } public function register() { //可以這么綁定,這需要use App; // App::bind('test',function() { // return new \App\Test\TestFacades; // }); //也可以這么綁定,推薦。這個(gè)test對(duì)應(yīng)于Facade的getFacadeAccessor返回值 $this->app->bind("test", function(){ return new MyFoo(); //給這個(gè)Facade返回一個(gè)代理實(shí)例。所有對(duì)Facade的調(diào)用都會(huì)被轉(zhuǎn)發(fā)到該類(lèi)對(duì)象下。 }); } }
Step 5 ? 在config/app.php注冊(cè)ServiceProvider類(lèi)
Step 6 ? 在config/app.php注冊(cè)自定義Facade的別名
使用測(cè)試:
Add the following lines in app/Http/routes.php.
Route::get('/facadeex', function(){ return TestFacades::testingFacades(); });
Step 9 ? Visit the following URL to test the Facade.
以上是“Laravel中Contracts和Facades有什么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。