Laravel 中使用的 Facades 是什么? [英] What is Facades used in Laravel?

查看:23
本文介绍了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:

  1. Why we really use use IlluminateSupportFacades?
  2. 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 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆