Laravel Facades
Facades提供了一个静态接口给服务容器中的类。Laravel Facades作为服务容器中的底层类的静态代理,提供了一个简洁,富有表现力的语法的好处,同时保持比传统的静态方法更多的可测试性和灵活性。
如何创建Facades
以下是在 Laravel 中创建 Facade 的步骤:
步骤1: 新建 PHP 类文件;
步骤2: 将该类绑定到Service Provider;
步骤3: 将该 ServiceProvider 作为提供者,注册到 config\app.php;
步骤4: 创建继承lluminate\Support\Facades\Facade的类;
步骤5: 给步骤4创建的类起别名,并注册到config\app.php。
Facades类参考
Laravel 附带了许多 Facade,下表显示了内置的 Facade 类引用:
Facade | 类 | 服务容器绑定 |
---|---|---|
App | Illuminate\Foundation\Application | app |
Artisan | Illuminate\Contracts\Console\Kernel | artisan |
Auth | Illuminate\Auth\AuthManager | auth |
Auth (Instance) | Illuminate\Auth\Guard | |
Blade | Illuminate\View\Compilers\BladeCompiler | blade.compiler |
Bus | Illuminate\Contracts\Bus\Dispatcher | |
Cache | Illuminate\Cache\Repository | cache |
Config | Illuminate\Config\Repository | config |
Cookie | Illuminate\Cookie\CookieJar | cookie |
Crypt | Illuminate\Encryption\Encrypter | encrypter |
DB | Illuminate\Database\DatabaseManager | db |
DB (Instance) | Illuminate\Database\Connection | |
Event | Illuminate\Events\Dispatcher | events |
File | Illuminate\Filesystem\Filesystem | files |
Gate | Illuminate\Contracts\Auth\Access\Gate | |
Hash | Illuminate\Contracts\Hashing\Hasher | hash |
Input | Illuminate\Http\Request | request |
Lang | Illuminate\Translation\Translator | translator |
Log | Illuminate\Log\Writer | log |
Illuminate\Mail\Mailer | mailer | |
Password | Illuminate\Auth\Passwords\PasswordBroker | auth.password |
Queue | Illuminate\Queue\QueueManager | queue |
Queue (Instance) | Illuminate\Queue\QueueInterface | |
Queue (Base Class) | Illuminate\Queue\Queue | |
Redirect | Illuminate\Routing\Redirector | redirect |
Redis | Illuminate\Redis\Database | redis |
Request | Illuminate\Http\Request | request |
Response | Illuminate\Contracts\Routing\ResponseFactory | |
Route | Illuminate\Routing\Router | router |
Schema | Illuminate\Database\Schema\Blueprint | |
Session | Illuminate\Session\SessionManager | session |
Session (Instance) | Illuminate\Session\Store | |
Storage | Illuminate\Contracts\Filesystem\Factory | filesystem |
URL | Illuminate\Routing\UrlGenerator | url |
Validator | Illuminate\Validation\Factory | validator |
Validator (Instance) | Illuminate\Validation\Validator | |
View | Illuminate\View\Factory | view |
View (Instance) | Illuminate\View\View |
示例
步骤1: 执行以下命令创建一个名为TestFacadesServiceProvider的服务提供者:
php artisan make:provider TestFacadesServiceProvider
步骤2: 执行成功后,会得到如下输出:
步骤3: 创建一个名为TestFacades.phpatApp/Test.
应用程序/测试/TestFacades.php
<?php namespace App\Test; class TestFacades{ public function testingFacades() { echo "Testing the Facades in Laravel."; } } ?>
步骤4: 新建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'; } }
步骤5: 新建App/Providers/TestFacadesServiceProviders.php文件,并添加以下代码:
<?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文件中添加服务提供者,如下图所示:
步骤7: 在config/app.php文件中添加别名,如下图所示:
步骤8: 修改application/config/routes.php文件,并在文件末尾添加以下配置:
Route::get('/facadeex', function() { return TestFacades::testingFacades(); });
步骤9: 访问以下 URL 来测试 Facade:
http://localhost:8000/facadeex
步骤10: 访问该 URL 后,你将收到以下输出: