Automatic Facade’s coming to Laravel 5.4

Tutorials

January 2nd, 2017

Automatic Facade’s coming to Laravel 5.4

Coming in Laravel 5.4 is a new feature that will allow you to use any class as a Facade on the fly. If you are not familiar with Laravel Facades here is what they are:

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.

Automatic Facade Example

In Taylor’s example from the announcement here is how it works:

1namespace App;
2
3class Zonda
4{
5 public function zurf()
6 {
7 return ‘Zurfing’;
8 }
9}

Then, in the routes or controller:

1use Facades\ {
2 App\Zonda
3};
4
5Route::get('/', function () {
6 return Zonda::zurf();
7});

Filed in:

Eric L. Barnes

Eric is the creator of Laravel News and has been covering Laravel since 2012.