[关闭]
@xzkcz 2015-05-11T02:32:08.000000Z 字数 1261 阅读 1594

Laravel Facade

laravel


There are three components to generating a Facade:
1. The Facade Root, the underlying class the Facade calls methods on.
2. The Facade Class, which tells Laravel which registered (underlying) class it pertains to
3. A Service Provider, which registers the underlying class in the App container

Underlying Facade

  1. // Fideloper/Example/Facades/UnderlyingClass.php
  2. <?php namespace Fideloper\Example\Facades;
  3. use Illuminate\Support\Facades\Facade;
  4. class UnderlyingClass extends Facade {
  5. protected static function getFacadeAccessor() { return 'underlyingclass'; }
  6. }

Underlying Class

  1. // Fideloper/Example/UnderlyingClass.php
  2. <?php namespace Fideloper\Example;
  3. class UnderlyingClass {
  4. public function doSomething(){
  5. echo 'Doing something!';
  6. }
  7. }

Underlying Service Provider

  1. // Fideloper/Example/UnderlyingServiceProvider.php
  2. <?php namespace Fideloper\Example;
  3. use Illuminate\Support\ServiceProvider;
  4. class UnderlyingServiceProvider extends ServiceProvider {
  5. public function boot(){
  6. $loader = \Illuminate\Foundation\AliasLoader::getInstance();
  7. $loader->alias('UnderlyingClass', 'Fideloper\Example\Facades\UnderlyingClass');
  8. }
  9. public function register(){
  10. $this->app['underlyingclass'] = $this->app->share(function($app){
  11. return new UnderlyingClass;
  12. });
  13. }
  14. }

Register Service Provider

Register your Service Provider in the app/config/app.php file

Using Facade

  1. UnderlyingClass::doSomething();
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注