×
Laravel教程Laravel环境安装配置Laravel应用程序结构Laravel配置Laravel路由Laravel中间件Laravel控制器Laravel请求Laravel CookieLaravel响应Laravel视图Laravel重定向Laravel操作数据库Laravel插入数据库表数据Laravel检索查询数据Laravel更新数据Laravel删除数据Laravel错误和日志记录Laravel表单处理Laravel本地化Laravel会话(session)Laravel验证Laravel文件上传Laravel发送邮件Laravel AjaxLaravel错误处理Laravel事件处理Laravel FacadesLaravel安全

Laravel Facades


Facades提供了一个“静态”的接口到应用程序的服务容器中可用的类。 Laravel 的“facades”作为“静态代理”在服务容器底层类,提供了一个简洁, 富有表现的语法,同时保持比传统的静态方法更有可测试性和灵活性。

如何创建Facade

以下是在Laravel创建 Facade 的步骤:
  • 第1步 - 创建PHP类文件
  • 第2步 - 绑定类到服务提供者
  • 第3步- 注册服务提供者到 Configapp.php 作为供应者
  • 第4步- 创建类,这个类是扩展lluminateSupportFacadesFacade
  • 第5步- 注册第4点到 Configapp.php 作为别名

Facade类参考

Laravel附带许多Facades。下面是内置的Facades类引用。
Facade
服务容器绑定
App IlluminateFoundationApplication app
Artisan IlluminateContractsConsoleKernel artisan
Auth IlluminateAuthAuthManager auth
Auth (Instance) IlluminateAuthGuard
Blade IlluminateViewCompilersBladeCompiler blade.compiler
Bus IlluminateContractsBusDispatcher
Cache IlluminateCacheRepository cache
Config IlluminateConfigRepository config
Cookie IlluminateCookieCookieJar cookie
Crypt IlluminateEncryptionEncrypter encrypter
DB IlluminateDatabaseDatabaseManager db
DB (Instance) IlluminateDatabaseConnection
Event IlluminateEventsDispatcher events
File IlluminateFilesystemFilesystem files
Gate IlluminateContractsAuthAccessGate
Hash IlluminateContractsHashingHasher hash
Input IlluminateHttpRequest request
Lang IlluminateTranslationTranslator translator
Log IlluminateLogWriter log
Mail IlluminateMailMailer mailer
Password IlluminateAuthPasswordsPasswordBroker auth.password
Queue IlluminateQueueQueueManager queue
Queue (Instance) IlluminateQueueQueueInterface
Queue (Base Class) IlluminateQueueQueue
Redirect IlluminateRoutingRedirector redirect
Redis IlluminateRedisDatabase redis
Request IlluminateHttpRequest request
Response IlluminateContractsRoutingResponseFactory
Route IlluminateRoutingRouter router
Schema IlluminateDatabaseSchemaBlueprint
Session IlluminateSessionSessionManager session
Session (Instance) IlluminateSessionStore
Storage IlluminateContractsFilesystemFactory filesystem
URL IlluminateRoutingUrlGenerator url
Validator IlluminateValidationFactory validator
Validator (Instance) IlluminateValidationValidator
View IlluminateViewFactory view
View (Instance) IlluminateViewView

示例

第1步- 执行以下命令创建一个叫作 TestFacadesServiceProvider 的服务提供者。
php artisan make:provider TestFacadesServiceProvider
第2步 - 成功执行后,您会收到以下输出 -

第3步 - 在“App/Test”创建一个名为 “TestFacades.php”的类

App/Test/TestFacades.php

<?php
namespace AppTest;

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

App/Test/Facades/TestFacades.php

<?php
namespace appTestFacades;
use IlluminateSupportFacadesFacade;

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

App/Providers/TestFacadesServiceProvider.php

<?php
namespace AppProviders;
use App;
use IlluminateSupportServiceProvider;

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

config/app.php


第7步 - 在文件 config/app.php 中添加别名如图所示如下图。

config/app.php


'TestFacades' => AppTestFacadesTestFacades::class, 

第8步 - 添加以下行到文件 - app/Http/routes.php

app/Http/routes.php

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

http://localhost:8000/facadeex

第10步 - 访问URL后,您会收到以下输出 -


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)