PHPackages                             lagdo/symfony-facades - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. lagdo/symfony-facades

ActiveLibrary[Framework](/categories/framework)

lagdo/symfony-facades
=====================

Call Symfony services using facades.

v3.1.1(7mo ago)1878.3k↓34.5%5BSD-3-ClausePHPPHP &gt;=8.2CI passing

Since Oct 21Pushed 7mo ago2 watchersCompare

[ Source](https://github.com/lagdo/symfony-facades)[ Packagist](https://packagist.org/packages/lagdo/symfony-facades)[ Docs](https://github.com/lagdo/symfony-facades)[ RSS](/packages/lagdo-symfony-facades/feed)WikiDiscussions main Synced 3d ago

READMEChangelog (10)Dependencies (10)Versions (19)Used By (0)

[![Build Status](https://github.com/lagdo/symfony-facades/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/lagdo/symfony-facades/actions)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/aa8d492acb0616aceaba20b1f0cd28f559a1c1ebb888c599bbed174ee4cda242/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6c6167646f2f73796d666f6e792d666163616465732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d61696e)](https://scrutinizer-ci.com/g/lagdo/symfony-facades/?branch=main)[![StyleCI](https://camo.githubusercontent.com/80de9e2300154d1898e4f8ac9ed8c71f4afb42b0534ed361ae4cb4978d2b053a/68747470733a2f2f7374796c6563692e696f2f7265706f732f3431383438383531332f736869656c643f6272616e63683d6d61696e)](https://styleci.io/repos/418488513)[![codecov](https://camo.githubusercontent.com/5b7745c0f28b50281920a6b2de241288973ad41d97e53b3999363d95629cf4d0/68747470733a2f2f636f6465636f762e696f2f67682f6c6167646f2f73796d666f6e792d666163616465732f6272616e63682f6d61696e2f67726170682f62616467652e7376673f746f6b656e3d4845524b433630434331)](https://codecov.io/gh/lagdo/symfony-facades)

[![Latest Stable Version](https://camo.githubusercontent.com/30f47666c7b4ad3913edd3cac7b80a85c353abb59e6b256304201f9aa7ff62d2/68747470733a2f2f706f7365722e707567782e6f72672f6c6167646f2f73796d666f6e792d666163616465732f762f737461626c65)](https://packagist.org/packages/lagdo/symfony-facades)[![Total Downloads](https://camo.githubusercontent.com/95f1b0a52b390c964ad5d97cc21e42798640b1380371295963b6775151958fb6/68747470733a2f2f706f7365722e707567782e6f72672f6c6167646f2f73796d666f6e792d666163616465732f646f776e6c6f616473)](https://packagist.org/packages/lagdo/symfony-facades)[![License](https://camo.githubusercontent.com/53492bcf3d9b8efb951430930bfd2bbd9009cf2bc11f44c7a989a32559ed5902/68747470733a2f2f706f7365722e707567782e6f72672f6c6167646f2f73796d666f6e792d666163616465732f6c6963656e7365)](https://packagist.org/packages/lagdo/symfony-facades)

Facades for Symfony services
============================

[](#facades-for-symfony-services)

With this package, Symfony services can be called using facades, with static method syntax.

It is a simpler alternative to passing services as parameters in the constructors of other classes, or using lazy services. It will be especially interesting in the case when a class depends on many services, but calls some of them only occasionally.

I have published this two parts articles to explain why and when to use service facades in a Symfony application.

- Part one:
- Part two:

### Facades definition

[](#facades-definition)

The base classes for service facade definitions are provided by the [lagdo/facades](https://github.com/lagdo/facades) package.

A service facade based on this package can be use without any change with other frameworks, if a package for this framework is available, or a `PSR-11` container can be provided.

The following packages are also available:

- Laravel (yes):
- CakePHP:
- Yii:

### Installation

[](#installation)

Install the package with `composer`. For Symfony version 6.\* or older, install the version 2.3 of the package. For Symfony version 7.\* and newer, install the version 3.\*.

```
composer require lagdo/symfony-facades
```

Register the `Lagdo\Symfony\Facades\FacadesBundle` bundle in the `config/bundles.php` file.

### Usage

[](#usage)

Note

If you are migrating from an older version to 3.1.0 or 2.4.0, please keep in mind that some classes have been moved or renamed.

```
// Before 3.1.0 or 2.4.0:
use Lagdo\Symfony\Facades\AbstractFacade;
use Lagdo\Symfony\Facades\ServiceInstance;
use Lagdo\Symfony\Facades\Log;

// After 3.1.0 or 2.4.0:
use Lagdo\Facades\AbstractFacade;
use Lagdo\Facades\ServiceInstance;
use Lagdo\Facades\Logger; // The facade class name has changed.
```

A facade inherits from the `Lagdo\Facades\AbstractFacade` abstract class, and implements the `getServiceIdentifier()` method, which must return the id of the corresponding service in the Symfony service container.

```
namespace App\Facades;

use App\Services\MyService;
use Lagdo\Facades\AbstractFacade;

/**
 * @extends AbstractFacade
 */
class MyFacade extends AbstractFacade
{
    /**
     * @inheritDoc
     */
    protected static function getServiceIdentifier(): string
    {
        return MyService::class;
    }
}
```

The methods of the `App\Services\MyService` service can now be called using the `App\Facades\MyFacade` facade, like this.

```
class TheService
{
    public function theMethod()
    {
        MyFacade::myMethod();
    }
}
```

Instead of this.

```
class TheService
{
    /**
     * @var MyService
     */
    protected $myService;

    public function __construct(MyService $myService)
    {
        $this->myService = $myService;
    }

    public function theMethod()
    {
        $this->myService->myMethod();
    }
}
```

The `@extends AbstractFacade` phpdoc will prevent errors during code analysis with [PHPStan](https://phpstan.org/), and allow code completion on calls to facades in editors.

### Using a service locator

[](#using-a-service-locator)

The above facade will work only for services that are declared as public.

In order to call private services with facades, a service locator with id `lagdo.facades.service_locator` must be declared in the `config/services.yaml` file. See the [Symfony service locators documentation](https://symfony.com/doc/4.4/service_container/service_subscribers_locators.html).

The private services that need to be accessed with a facade must be passed as arguments to the service locator. For each argument, the key is the service id in the facade, while the value is the service id in the container.

In the following example, the `Twig` service is passed to the service locator.

```
    lagdo.facades.service_locator:
        public: true
        class: Symfony\Component\DependencyInjection\ServiceLocator
        tags: ['container.service_locator']
        arguments:
            -
                Twig\Environment: '@twig'
```

A facade can then be defined for the `Twig` service.

```
namespace App\Facades;

use Lagdo\Facades\AbstractFacade;
use Twig\Environment;

/**
 * @extends AbstractFacade
 */
class View extends AbstractFacade
{
    /**
     * @inheritdoc
     */
    protected static function getServiceIdentifier(): string
    {
        return Environment::class;
    }
}
```

Templates can now be rendered using the facade.

```
use App\Facades\View;

class TheService
{
    public function theMethod()
    {
        ...
        $html = View::render($template, $vars);
        ...
    }
}
```

### The `lagdo.facades.service` tag

[](#the-lagdofacadesservice-tag)

Starting from version 2.3.0, the private services that need to be accessed with a facade can be tagged with `lagdo.facades.service`.

These services will then be automatically passed to the service locator, together with those received as arguments.

In the following example, the `Twig` and `App\Services\TaggedService` services will be passed to the service locator.

```
    lagdo.facades.service_locator:
        public: true
        class: Symfony\Component\DependencyInjection\ServiceLocator
        tags: ['container.service_locator']
        arguments:
            -
                Twig\Environment: '@twig'

    App\Services\TaggedService:
        tags: [lagdo.facades.service]
        class: App\Services\TaggedService
```

A facade can then be defined for the service.

```
namespace App\Facades;

use App\Services\TaggedService;
use Lagdo\Facades\AbstractFacade;

/**
 * @extends AbstractFacade
 */
class TaggedServiceFacade extends AbstractFacade
{
    /**
     * @inheritdoc
     */
    protected static function getServiceIdentifier(): string
    {
        return TaggedService::class;
    }
}
```

### Getting the service instance

[](#getting-the-service-instance)

The `instance()` method of a service facade returns the instance of the linked service.

```
class TheService
{
    public function theMethod()
    {
        $service = MyFacade::instance();
        $service->myMethod();
    }
}
```

### The `Lagdo\Facades\ServiceInstance` trait

[](#the-lagdofacadesserviceinstance-trait)

By default, each call to a facade method will also call the Symfony service container. The service instance can be saved in the facade after the first call to the Symfony service container, using the `Lagdo\Facades\ServiceInstance` trait. The next calls with return the service instance without calling the Symfony service container.

```
namespace App\Facades;

use App\Services\MyService;
use Lagdo\Facades\AbstractFacade;
use Lagdo\Facades\ServiceInstance;

/**
 * @extends AbstractFacade
 */
class MyFacade extends AbstractFacade
{
    use ServiceInstance;

    /**
     * @inheritDoc
     */
    protected static function getServiceIdentifier(): string
    {
        return MyService::class;
    }
}
```

Important

The `Lagdo\Facades\ServiceInstance` trait *must* be used in each service facade class, and not in a parent class. The same instance will be shared by all the classes inheriting the same base class using the trait, and the service facades will ot work as expected.

The Symfony service container is called only once in this example code.

```
    MyFacade::myMethod1(); // Calls the Symfony service container
    MyFacade::myMethod2(); // Doesn't call the Symfony service container
    MyFacade::myMethod1(); // Doesn't call the Symfony service container
```

### Provided facades

[](#provided-facades)

This package includes facades for some Symfony services.

#### Logger

[](#logger)

The `logger` service must be passed to the service locator in the `config/services.yaml` file.

```
    lagdo.facades.service_locator:
        public: true
        class: Symfony\Component\DependencyInjection\ServiceLocator
        tags: ['container.service_locator']
        arguments:
            -
                Psr\Log\LoggerInterface: '@logger'
```

Messages can now be logged using the facade.

```
use Lagdo\Facades\Logger;

Logger::info($message, $vars);
```

#### View

[](#view)

The `twig` service must be passed to the service locator in the `config/services.yaml` file.

```
    lagdo.facades.service_locator:
        public: true
        class: Symfony\Component\DependencyInjection\ServiceLocator
        tags: ['container.service_locator']
        arguments:
            -
                Twig\Environment: '@twig'
```

Views can now be rendered using the facade.

```
use Lagdo\Symfony\Facades\View;

$html = View::render($template, $vars);
```

Contribute
----------

[](#contribute)

- Issue Tracker: github.com/lagdo/symfony-facades/issues
- Source Code: github.com/lagdo/symfony-facades

License
-------

[](#license)

The package is licensed under the 3-Clause BSD license.

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance65

Regular maintenance activity

Popularity41

Moderate usage in the ecosystem

Community13

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 98.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~88 days

Recently: every ~75 days

Total

18

Last Release

210d ago

Major Versions

v1.1.0 → v2.0.02023-01-31

v2.3.1 → v3.0.02024-11-21

v2.3.2 → v3.0.12025-02-09

v2.x-dev → v3.1.12025-12-06

PHP version history (3 changes)v2.3.1PHP &gt;=7.4.0

v3.0.0PHP &gt;=8.2

v2.4.0PHP &gt;=8.0.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/ebb668d7515d81741c86e42cf3194f5623636fbb15dd5412e26ea8320865d229?d=identicon)[lagdo](/maintainers/lagdo)

---

Top Contributors

[![feuzeu](https://avatars.githubusercontent.com/u/15174329?v=4)](https://github.com/feuzeu "feuzeu (90 commits)")[![raffaelecarelle](https://avatars.githubusercontent.com/u/15015792?v=4)](https://github.com/raffaelecarelle "raffaelecarelle (1 commits)")

---

Tags

phpsymfonyframeworkfacadesservices

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/lagdo-symfony-facades/health.svg)

```
[![Health](https://phpackages.com/badges/lagdo-symfony-facades/health.svg)](https://phpackages.com/packages/lagdo-symfony-facades)
```

###  Alternatives

[laravel/framework

The Laravel Framework.

34.8k543.8M20.1k](/packages/laravel-framework)[symfony/symfony

The Symfony PHP framework

31.4k87.2M2.2k](/packages/symfony-symfony)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.4M203](/packages/sulu-sulu)[oro/platform

Business Application Platform (BAP)

645143.5k115](/packages/oro-platform)[kimai/kimai

Kimai - Time Tracking

4.8k9.0k1](/packages/kimai-kimai)[contao/core-bundle

Contao Open Source CMS

1231.6M2.8k](/packages/contao-core-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
