Laravel教程:“静态代理”Facades类参考与实例

2018-10-0308:33:00后端程序开发Comments1,919 views字数 3190阅读模式
Facades提供了一个“静态”的接口到应用程序的服务容器中可用的类。 Laravel 的“facades”作为“静态代理”在服务容器底层类,提供了一个简洁, 富有表现的语法,同时保持比传统的静态方法更有可测试性和灵活性。
如何创建Facade
以下是在Laravel创建 Facade 的步骤:
  • 第1步 - 创建PHP类文件
  • 第2步 - 绑定类到服务提供者
  • 第3步- 注册服务提供者到 Config\app.php 作为供应者
  • 第4步- 创建类,这个类是扩展lluminate\Support\Facades\Facade
  • 第5步- 注册第4点到 Config\app.php 作为别名
Facade类参考
Laravel附带许多Facades。下面是内置的Facades类引用。
Facade
服务容器绑定
AppIlluminate\Foundation\Applicationapp
ArtisanIlluminate\Contracts\Console\Kernelartisan
AuthIlluminate\Auth\AuthManagerauth
Auth (Instance)Illuminate\Auth\Guard
BladeIlluminate\View\Compilers\BladeCompilerblade.compiler
BusIlluminate\Contracts\Bus\Dispatcher
CacheIlluminate\Cache\Repositorycache
ConfigIlluminate\Config\Repositoryconfig
CookieIlluminate\Cookie\CookieJarcookie
CryptIlluminate\Encryption\Encrypterencrypter
DBIlluminate\Database\DatabaseManagerdb
DB (Instance)Illuminate\Database\Connection
EventIlluminate\Events\Dispatcherevents
FileIlluminate\Filesystem\Filesystemfiles
GateIlluminate\Contracts\Auth\Access\Gate
HashIlluminate\Contracts\Hashing\Hasherhash
InputIlluminate\Http\Requestrequest
LangIlluminate\Translation\Translatortranslator
LogIlluminate\Log\Writerlog
MailIlluminate\Mail\Mailermailer
PasswordIlluminate\Auth\Passwords\PasswordBrokerauth.password
QueueIlluminate\Queue\QueueManagerqueue
Queue (Instance)Illuminate\Queue\QueueInterface
Queue (Base Class)Illuminate\Queue\Queue
RedirectIlluminate\Routing\Redirectorredirect
RedisIlluminate\Redis\Databaseredis
RequestIlluminate\Http\Requestrequest
ResponseIlluminate\Contracts\Routing\ResponseFactory
RouteIlluminate\Routing\Routerrouter
SchemaIlluminate\Database\Schema\Blueprint
SessionIlluminate\Session\SessionManagersession
Session (Instance)Illuminate\Session\Store
StorageIlluminate\Contracts\Filesystem\Factoryfilesystem
URLIlluminate\Routing\UrlGeneratorurl
ValidatorIlluminate\Validation\Factoryvalidator
Validator (Instance)Illuminate\Validation\Validator
ViewIlluminate\View\Factoryview
View (Instance)Illuminate\View\View

示例

第1步- 执行以下命令创建一个叫作 TestFacadesServiceProvider 的服务提供者。

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6253.html

php artisan make:provider TestFacadesServiceProvider
第2步 - 成功执行后,您会收到以下输出 -
Laravel教程:“静态代理”Facades类参考与实例
第3步 - 在“App/Test”创建一个名为 “TestFacades.php”的类

App/Test/TestFacades.php文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6253.html

<?php
namespace App\Test;

class TestFacades{
   public function testingFacades(){
      echo "Testing the Facades in Laravel.";
   }
}
第4步 - 在“App/Test/Facades”创建一个名为“TestFacades.php” 的一个Facade类。

App/Test/Facades/TestFacades.php文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6253.html

<?php
namespace app\Test\Facades;
use Illuminate\Support\Facades\Facade;

class TestFacades extends Facade{
   protected static function getFacadeAccessor() { return 'test'; }
}
第5步- 在“App/Test/Facades”创建一个名为 “TestFacadesServiceProviders.php”的一个Facade类。

App/Providers/TestFacadesServiceProvider.php文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6253.html

<?php
namespace App\Providers;
use App;
use Illuminate\Support\ServiceProvider;

class TestFacadesServiceProvider extends ServiceProvider {
   public function boot() {
      //
   }
   public function register() {
      App::bind('test',function() {
         return new \App\Test\TestFacades;
      });
   }
}
第6步 - 在文件 config/app.php 中添加一个服务提供者如图所示如下图。

config/app.php文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6253.html

Laravel教程:“静态代理”Facades类参考与实例
第7步 - 在文件 config/app.php 中添加别名如图所示如下图。

config/app.php文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6253.html

Laravel教程:“静态代理”Facades类参考与实例
'TestFacades' => App\Test\Facades\TestFacades::class,
第8步 - 添加以下行到文件 - app/Http/routes.php

app/Http/routes.php文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6253.html

Route::get('/facadeex', function(){
   return TestFacades::testingFacades();
});
第9步 - 访问以下网址测试 Facade

http://localhost:8000/facadeex文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6253.html

第10步 - 访问URL后,您会收到以下输出 -
Laravel教程:“静态代理”Facades类参考与实例
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6253.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/6253.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定