Automatic facades in Laravel

How to make facade real-time generating?

To call regular methods statically, you can use facades. From Laravel 5.5 you can use real-time facades to call these methods without creating any class.

This page shows an example of using this feature, so you can make sure that it is very convenient.

Example with creating a facade class

Let's say we have a StringFilter class that is located in the /app/Helpers/Filters directory and has a single method-keepNumbersAndLettersOnly(), which removes all characters from the string except Latin letters and numbers:

1 2 3 4 5 6 7 8 9 10 11
<?php   namespace App\Helpers\Filters;   class StringFilter { public function keepNumbersAndLettersOnly(string $str): string { return preg_replace('/[^A-Za-z0-9]+/u', '', $str); } }

To use this method without creating a class instance manually, we can create a facade:

1 2 3 4 5 6 7 8 9 10 11 12 13
<?php   namespace App\Facades\Helpers\Filters;   use Illuminate\Support\Facades\Facade;   class StringFilter extends Facade { protected static function getFacadeAccessor() { return \App\Helpers\Filters\StringFilter::class; } }

And register an alias in /config/app.php by adding the following line to the array with the alias key:

'StringFilter' => \App\Facades\Helpers\Filters\StringFilter::class,

Now we can access the keepNumbersAndLettersOnly() method via the facade alias:

\StringFilter::keepNumbersAndLettersOnly('abc-=|,.123');

Or by importing the namespace of the facade:

1 2 3 4 5 6 7 8 9 10 11 12 13
<?php   namespace App\Http\Controllers;   use App\Facades\Helpers\Filters\StringFilter;   class TestController extends Controller { public function index() { return StringFilter::keepNumbersAndLettersOnly('abc-=|,.123'); } }

Example of real-time facade

Let's repeat the same thing, but without creating a facade class.

All you need to do is just add the prefix Facades/ to the beginning of the imported namespace of the required class.

This way we can access the regular methods of the class in a static way, without creating an instance of the class with our own hands:

1 2 3 4 5 6 7 8 9 10 11 12 13
<?php   namespace App\Http\Controllers;   use Facades\App\Helpers\Filters\StringFilter;   class TestController extends Controller { public function index() { return StringFilter::keepNumbersAndLettersOnly('abc-=|,.123'); } }

How can we not love Laravel for such magic? 🤗

Adding an alias for a real-time facade

Let's go even further and get rid of the need to import the namespace with the Facades/ prefix every time.

To do this, in the same way, you need to add a single line in the /config/app.php in an array with the alias key:

'StringFilter' => Facades\App\Helpers\Filters\StringFilter::class,

Now we can call methods using the alias of our automatically generated facade in this way:

1 2 3 4 5 6 7 8 9 10 11
<?php   namespace App\Http\Controllers;   class TestController extends Controller { public function index() { return \StringFilter::keepNumbersAndLettersOnly('abc-=|,.123'); } }

Comments

Spelling error report

The following text will be sent to our editors: