为什么我的 Laravel 安装需要前缀 \ 反斜杠作为 facades 别名

Why does my Laravel installation need prefixing \ backslash for facades aliases

我不知道下面例子出错的原因:

Auth::user()->id 

但是,只要在它前面加上反斜杠就可以正常工作:

\Auth::user()->id

这是来自 config/app.php

的片段
'aliases' => [

        'App' => Illuminate\Support\Facades\App::class,
        'Artisan' => Illuminate\Support\Facades\Artisan::class,
        'Auth' => Illuminate\Support\Facades\Auth::class,
        'Blade' => Illuminate\Support\Facades\Blade::class,
        'Broadcast' => Illuminate\Support\Facades\Broadcast::class,

服务器是 Ubuntu 16.04

上的 Apache

那是 how namespaces work

当您在典型的 Laravel 模型、控制器等中时,您在其命名空间内 - 类似于 AppApp\Http\Controllers 或诸如此类的东西。因此,Auth::foo() 分别表示 App\Auth::foo()App\Http\Controllers\Auth::foo()

这就是为什么 the examples that teach you how to use Auth 在使用 Auth class 之前都会做 use Illuminate\Support\Facades\Auth;,并说这样的话:

We will access Laravel's authentication services via the Auth facade, so we'll need to make sure to import the Auth facade at the top of the class.