AlexP11223 AlexP11223 - 15 days ago 7
PHP Question

What is the correct way to use/import Laravel facades?

What is the correct way to import namespace for facades (such as


Do I need to use import like
use Illuminate\Support\Facades\Hash

I saw some people using them as
(from namespaced file, such as controllers created by default) or
(from non-namespaced file, such as routes).

Also ide-helper generates facades in the root namespace:

namespace {
exit("This file should not be included, only analyzed by your IDE");

class Hash extends \Illuminate\Support\Facades\Hash{
/** ... */
public static function make($value, $options = array()){
return \Illuminate\Hashing\BcryptHasher::make($value, $options);

But I don't understand why it works if this class is in
namespace, not in the root namespace.


You are comparing Facades to Aliases.

The Illuminate\Support\Facades\Hash is a facade class, but \Hash is an alias class to that facade. Have a look at your config/app.php and see how they're being mapped:

'aliases' => [
    'App'           => Illuminate\Support\Facades\App::class,
    'Artisan'       => Illuminate\Support\Facades\Artisan::class,
    'Auth'          => Illuminate\Support\Facades\Auth::class,
    // ...

You can use either, \Hash alias class or Illuminate\Support\Facades\Hash facade class. Both are correct. Clearly, using \Hash alias is a lot easier.

In the bootstrapping phase, Laravel uses a service named AliasLoader. It takes the aliases array from the config/app.php, iterates over all the elements, and creates a queue of __autoload functions using PHP's spl_autoload_register. Each __autoload function is responsible for creating an alias for the respective facade class by using PHP’s class_alias function. As the result, we won’t have to import and alias the classes before using them.

Read more:
How Laravel Facades Work and How to Use Them Elsewhere