Facades为应用程序服务容器中可用的类提供静态接口. Laravel facades 充当静态代理到服务容器中的底层类,提供简洁,富有表现力的语法,同时保持比传统静态方法更多的可测试性和灵活性.
如何创建Facade
以下是在Laravel中创建Facade的步骤 :
第1步 : 创建PHP类文件.
步骤2 : 将该课程绑定到服务提供商.
第3步 : 将ServiceProvider注册到
Config\app.php作为提供者.
步骤4 : 创建此类的类扩展到
lluminate \Support\Facades \Facade.
第5步 : 将第4点注册到Config\app.php作为别名.
门面类参考
Laravel船上有许多外墙.下表显示了内置的Facade类引用 :
Facade | Class | 服务容器绑定 |
---|---|---|
App | Illuminate\Foundation\Application | app |
Artisan | Illuminate\Contracts\Console\Kernel | artisan |
Auth | Illuminate\Auth\AuthManager | auth |
Auth(实例) | Illuminate\Auth\Guard | |
Blade | Illuminate\View \ Compilers \ Bradler | blade.compiler |
Bus | Illuminate\Contracts\\ \\ brus \Dispatcher | |
缓存 | 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(实例) | Illuminate\Database\Connection | |
活动 | Illuminate\Events\Dispatcher | events |
文件 | 照亮\文件系统\文件系统 | 文件 |
Gate | Illuminate\Contracts\Auth\Access\Gate | |
Hash | Illuminate \ Contontcts \ Hashing \Hasher | hash |
输入 | Illuminate\Http\Request | request |
Lang | Illuminate\Translation\Translator | 翻译 |
Log | Illuminate\Log\Writer | log |
Illuminate\Mail \ Mailer | mailer | |
密码 | Illuminate\Auth\Passwords\PasswordBroker | auth.password |
Queue | Illuminate\Queue \QueueManager | queue |
队列(实例) | Illuminate\Queue \QueueInterface | |
队列(基类) | Illuminate\Queue \Queue | |
重定向 | Illuminate\Routing \ Redirector | redirect |
Redis | Illuminate\Redis \Database | redis |
请求 | Illuminate\Http\Request | request |
回复 | Illuminate\Contracts\Routing \ResponseFactory | |
路线 | Illuminate\Routing\Router | router |
Schema | Illuminate\Database\Schema \Blueprint | |
会话 | Illuminate\Session\SessionManager | session |
会话(实例) | 照亮\ S ession \Store | |
存储 | Illuminate\Contracts\Filesystem\Factory | filesystem |
URL | Illuminate\Routing\UrlGenerator | url |
Validator | Illuminate\Validation\Factory | validator |
Validator(Instance) | Illuminate\Validation\Validator | |
查看 | 照亮\查看\ Factory | view |
查看(实例) | Illuminate\View \ View |
示例
第1步 : 通过执行以下命令创建名为 TestFacadesServiceProvider 的服务提供者.
php artisan make:provider TestFacadesServiceProvider
第2步 : 成功执行后,您将收到以下输出 :
第3步 : 在 App/Test 创建一个名为 TestFacades.php 的类.
App/Test/TestFacades.php
第4步 : 在"App/Test/Facades"创建名为"TestFacades.php"的Facade类.
App/Test/Facades/TestFacades.php
第5步 : 在 App/Test/Facades创建一个名为 TestFacadesServiceProviders.php 的Facade类.
App/Providers/TestFacadesServiceProviders.php
第6步 : 在文件 config/app.php 中添加服务提供商,如下图所示.
config/app.php
第7步 : 在文件 config/app.php 中添加别名,如下图所示.
config/app.php
步骤8 : 在 app/Http/routes.php中添加以下行.
app/Http/routes.php
Route::get('/facadeex', function() { return TestFacades::testingFacades();});第9步 : 访问以下URL以测试Facade.
http://localhost:8000/facadeex第10步 : 访问URL后,您将收到以下输出 :