search
HomeBackend DevelopmentPHP TutorialLaravel5学习四:Facade的运行机制

什么是Facades

官方文档如是说:

Facades 提供一个 静态接口 给在应用程序的 服务容器 中可以取用的类。Laravel 附带许多 facades,甚至你可能已经在不知情的状况下使用过它们!Laravel 的「facades」作为在 IoC 容器里面的基础类的静态代理,提供的语法有简洁、易表达的优点,同时维持比传统的静态方法更高的可测试性和弹性。

说实话,这段话读起来真不像人类的语言,我准备来拆解一下。
首先, Facades 是一个类,是一个什么类呢?它是基础类的一个静态代理。
其次, Facades 是可以直接从IOC容器中拿到的,对应到laravel5就是说,可以直接使用,它已经存在于app这个容器中。
最后, 为什么要有 Facades 这个东东?首先优点是:它提供的语法更简洁,易表达。这个其实不是主要原因,个人觉得最主要的原因是:便于可测试性与降低耦合性(代理模式的优点)

用例子来说明

PS:本部分内容参考自网络:原文地址

我们知道$app 在laravel中是一个特殊的全局变量,它在最初启动的时候就是ioc容器的对象实例。
于是如果我们要取从ioc容器中取一个实例出来,是这样写的:

$value = $app->make('cache')->get('key');

由于$app implements PHP的 ArrayAccess interface,所以可以更简化点,写成:

$value = $app['cache']->get('key');

你甚至可以用函数的方式写成这样:

$value = app('cache')->get('key');

这个时候Facace跳出来说,我可以写成这样:

$value = Cache::get('key');
运行机制分析
  • Facades 的命名空间
    相信只要玩过laravel的人,都会见过当遇到 Facades 的时候,导入命名空间,只需要用 use XXX 即可。不用把完整的的命名空间写出来。这是为什么?
    正常的类的命名空间,应该这样写:
  • use Illuminate\Support\Facades\Cache;

    Facades 只需要这样写:

    use Cache;

    为什么能够这样?我看了一些它的运行过程,解析如下:

    这幅图,中的关键点是:AliasLoader中注册的自动加载方法,它使用了我们在app.php配置中的 aliases 数组,从而通过这个别名,可以找到对应的真实类存在的文件,然后完成对象的实例化。

  • 如何构建自己的 Facades
    PS:还是以 Cache 为例子
  • 首先需要构建的就是Cache这个类本身,实现它的功能。这里对应的文件是:
    Illuminate\Cache\CacheManager (备注:由于Cache有多种实现,因此通过管理者来获取想要的Cache实现方案。)

    当Cache这个类,写好之后,我们要将它绑定到一个服务提供者里边去,这里的服务提供者位置是:
    Illuminate\Cache\CacheServiceProvider ,绑定部分代码是:

    public function register() {    $this->app->singleton('cache', function ($app) {        return new CacheManager($app);    });    ......    }

    这里需要注意的是,绑定时,为什么要使用’cache’ 这个字符串,后面会讲到。当服务提供者写好后,需要在 app.php 的 providers 的数组中配置服务提供者,此处代码如下:

    'providers' => [    /* * Laravel Framework Service Providers... */    Illuminate\Foundation\Providers\ArtisanServiceProvider::class, Illuminate\Auth\AuthServiceProvider::class, Illuminate\Broadcasting\BroadcastServiceProvider::class, Illuminate\Bus\BusServiceProvider::class, Illuminate\Cache\CacheServiceProvider::class, ...... ]

    完成以下步骤后,就可以构建 Facades 类了,Cache 的 Facades 的位置在 Illuminate\Support\Facades\Cache 。

    <?phpnamespace Illuminate\Support\Facades;/** * @see \Illuminate\Cache\CacheManager * @see \Illuminate\Cache\Repository */class Cache extends Facade {    /** * Get the registered name of the component. * * @return string */    protected static function getFacadeAccessor() {        return 'cache';    }}

    所有的 Facades 类都需要继承自 Facades ,并实现 getFacadeAccessor 方法。它的工作是定义要从容器解析什么。这也就是说,如果我们绑定的时候,写的是 cache ,那么这里也需要返回 cache ,要不然解析的时候,就会找不到。大家可以试试改成其它名字。
    最后一步,需要配置 app.php 中的 aliases 数组,这涉及到 Facades 的自动加载。就是最开始画的那幅图。

    'aliases' => [    'App'       => Illuminate\Support\Facades\App::class, 'Artisan' => Illuminate\Support\Facades\Artisan::class, 'Auth' => Illuminate\Support\Facades\Auth::class, 'Blade' => Illuminate\Support\Facades\Blade::class, 'Bus' => Illuminate\Support\Facades\Bus::class, 'Cache' => Illuminate\Support\Facades\Cache::class, ...... ]
    Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

    What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

    PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

    PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

    PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

    PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

    PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

    PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

    Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

    PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

    PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

    PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

    PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

    PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

    How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

    PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    Atom editor mac version download

    Atom editor mac version download

    The most popular open source editor

    VSCode Windows 64-bit Download

    VSCode Windows 64-bit Download

    A free and powerful IDE editor launched by Microsoft

    Safe Exam Browser

    Safe Exam Browser

    Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment