@xzkcz
2015-05-11T02:32:08.000000Z
字数 1261
阅读 1594
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
// Fideloper/Example/Facades/UnderlyingClass.php
<?php namespace Fideloper\Example\Facades;
use Illuminate\Support\Facades\Facade;
class UnderlyingClass extends Facade {
protected static function getFacadeAccessor() { return 'underlyingclass'; }
}
// Fideloper/Example/UnderlyingClass.php
<?php namespace Fideloper\Example;
class UnderlyingClass {
public function doSomething(){
echo 'Doing something!';
}
}
// Fideloper/Example/UnderlyingServiceProvider.php
<?php namespace Fideloper\Example;
use Illuminate\Support\ServiceProvider;
class UnderlyingServiceProvider extends ServiceProvider {
public function boot(){
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('UnderlyingClass', 'Fideloper\Example\Facades\UnderlyingClass');
}
public function register(){
$this->app['underlyingclass'] = $this->app->share(function($app){
return new UnderlyingClass;
});
}
}
Register your Service Provider in the app/config/app.php file
UnderlyingClass::doSomething();