問題描述
我對 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 是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!