What is Facades used in Laravel?(Laravel 中使用的 Facades 是什么?)
问题描述
我对 Laravel 提供的 Facades 感到困惑.
Laravel 中的外墙
Facades 提供了一个静态"的结构.应用程序的服务容器中可用的类的接口.Laravel 附带了许多门面,可以访问几乎所有 Laravel 的功能.Laravel 外观充当静态代理"到服务容器中的底层类,提供简洁、富有表现力的语法的好处,同时比传统的静态方法保持更多的可测试性和灵活性.
Facades 在 Laravel 中是如何实现的
容器内的每个服务都有一个唯一的名称.在 Laravel 应用程序中,要直接从容器访问服务,我们可以使用 App::make()
方法或 app() 辅助函数.
methodName();
在 Laravel 中,所有服务都有一个外观类.这些 Facade 类扩展了 Facade 基类,它是 Illuminate/Support
包的一部分.他们唯一需要实现的是 getFacadeAccessor 方法,该方法返回容器内的服务名称.
I'm confused by the Facades offered by Laravel.
The Laravel documentation states:
Facades provide a "static" interface to classes that are available in the application's service container. Laravel ships with many facades which provide access to almost all of Laravel's features. Laravel facades serve as "static proxies" to underlying classes in the service container, providing the benefit of a terse, expressive syntax while maintaining more testability and flexibility than traditional static methods.
Please help me to understand:
- Why we really use
use IlluminateSupportFacades
? - How to create custom Facades ?
Props to SitePoint for sharing such informative and helpful knowledge about facades in Laravel.
The facade pattern is a software design pattern that is often used in object-oriented programming.
A facade is a class wrapping a complex library to provide a simpler and more readable interface to it.
Facades in Laravel
Facades provide a "static" interface to classes that are available in the application's service container. Laravel ships with many facades which provide access to almost all of Laravel's features. Laravel facades serve as "static proxies" to underlying classes in the service container, providing the benefit of a terse, expressive syntax while maintaining more testability and flexibility than traditional static methods.
How Facades Are implemented in Laravel
Every service inside the container has a unique name. In a Laravel application, to access a service directly from the container, we can use the App::make()
method or the app() helper function.
<?php
App::make('some_service')->methodName();
In Laravel, all services have a facade class. These facade classes extend the base Facade class which is part of the Illuminate/Support
package. The only thing that they need to implement is the getFacadeAccessor method, which returns the service name inside the container.
这篇关于Laravel 中使用的 Facades 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel 中使用的 Facades 是什么?
基础教程推荐
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01