Magento 2 Facades

Yegor Shytikov
3 min readOct 31, 2024

--

Magento Facades provide a static interface to classes. Mage 2 Magento library ships with many facades, providing access to almost all of Magento 2 Features.

Magento facades serve as “static proxies” to underlying classes, providing the benefit of a terse, expressive syntax while maintaining more testability and flexibility than traditional static methods. It’s perfectly fine if you don’t totally understand how the Magento 2 core framework works.

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.

Helper Functions

To complement facades, Mage 2 offers a variety of global “helper functions” that make it even easier to interact with standard Magento 2 features. Some of the familiar helper functions you may interact with are view, product, cache, config, URL, and more. Each helper function offered by Mage 2 is documented with their corresponding feature; however, a complete list is available within the dedicated helper documentation.

For example, we may simply use the function instead of using the Illuminate\Support\Facades\Response facade to generate a JSON response. Because helper functions are globally available, you do not need to import any classes to use them:

When to Utilize Facades

Facades have many benefits. They provide a terse, memorable syntax that allows you to use Magento 2 features without remembering long class names that must be injected or configured manually. Furthermore, their unique usage of PHP’s dynamic methods makes them easy to test.

However, some care must be taken when using facades. The primary danger of facades is class “scope creep”. Since facades are so easy to use and do not require DI injection, it can be easy to let your classes continue to grow and use many facades in a single class. If your Magento class is getting too large (500 lines of the magento code), consider splitting it into multiple smaller classes.

Magento 2 Facades vs. Dependency Injection

One of dependency injection's primary benefits is the ability to swap implementations of the injected class. This is useful during testing since you can inject a mock or stub and assert that various methods were called on the stub.

How Magento Mage2 Facades Work

A facade is a class that provides quick and easy access to some functionality. The machinery that makes this work is in the Facade class. Magento 2 facades and any custom facades you create will extend the base Facade class.

By checking this code, one might assume that the static get method is called on the Cache class:

use \Mage; \\ or without use than access by the full class path

Cache::get($key);

Or helper function

cache($key);

Magento 2 Core Alternative Code

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$cacheManager = $objectManager->get('\Magento\Framework\App\CacheInterface');
$cacheManager->clean($key);

//or you need cache class signature add DI to the cache calls after recompile the code and after use the cache object

Magento 1 example

Mage::app()->getCache()->get($key);

And the same applies to all Magento 2 features.

In the above diagram,

  • Structuring a system into subsystems helps reduce complexity.
  • A common design goal is to minimize the communication and dependencies between subsystems.
  • One way to achieve this goal is to introduce a Facade object that provides a single simplified interface to the more general facilities of the system

Magento 2 Mage 2 Facade and not only module:

--

--

Yegor Shytikov
Yegor Shytikov

Written by Yegor Shytikov

True Stories about Magento 2. Melting down metal server infrastructure into cloud solutions.

No responses yet