Wednesday, November 4, 2015

How Laravel Facades Work and How to Use Them Elsewhere

The Facade pattern is a software design pattern which is often used in object oriented programming. A facade is, in fact, a class wrapping a complex library to provide a simpler and more readable interface to it. The Facade pattern can also be used to provide a unified and well-designed API to a group of complex and poorly designed APIs.

Facades diagram

The Laravel framework has a feature similar to this pattern, also termed Facades. In this tutorial we will learn how to bring Laravel’s “Facades” to other frameworks. Before we continue, you need to have a basic understanding of Ioc containers.

Let’s first go through the inner working parts of Laravel’s facades, and then we’ll discuss how we can adapt this feature to the other environments.

Facades in Laravel

A Laravel facade is a class which provides a static-like interface to services inside the container. These facades, according to the documentation, serve as a proxy for accessing the underlying implementation of the container’s services.

There have been many debates in the PHP community about this naming, though. Some have argued that is term should be changed in order to avoid confusion of developers, as it doesn’t fully implement the Facade pattern. If this naming causes confusion for you, feel free to call it whatever name that feels right to you. But please note that the base class that we’re going to use is called Facade in the Laravel framework.

How Facades Are Implemented in Laravel

As you probably know, 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.

Continue reading %How Laravel Facades Work and How to Use Them Elsewhere%


by Reza Lavaryan via SitePoint

No comments:

Post a Comment