1

I have built a Laravel 5.4 application with a custom facade, App\Facades\Repo. The Repo facade proxies to a RepositoryFactory class. In my app configuration I have an alias Repo which points to the Repo facade. I use it to get repositories with calls such as Repo::get('User').

This works fine if I am in a controller or the routes file. In other parts of the application however, I can't use the Repo alias. The interpreter looks for a Repo class in the current namespace, and errors out. This raises two questions:

  • Which classes are aware of the facade aliases? What defines them?
  • In classes that are not aware of facade aliases, should I go ahead and import the facade class itself? Or is this a code smell?

By way of example, classes which are not alias-aware include my repositories themselves. I have created a super-type for them, but they don't inherit from any Laravel class. Sometimes my repositories need to call on other repositories to do their work.

DatsunBing
  • 8,684
  • 17
  • 87
  • 172

1 Answers1

1

What I noticed so far (working with Laravel 5.2) is that when you are inside a namespaced context you need to either have use Repo; or precede the facade with a slash, like \Repo::get("User").

Out of a namespaced context, like in routes.php, config files, or views, the facade works directly.

But this has more to do with how PHP works than with Laravel itself.

Thiago Barcala
  • 6,463
  • 2
  • 20
  • 23