【问题标题】:Laravel cannot find vendor classLaravel 找不到供应商类
【发布时间】:2016-10-28 06:10:33
【问题描述】:

我正在尝试使用这个包

https://packagist.org/packages/skagarwal/google-places-api#user-content-laravel-usage

我已经按照说明设置了服务提供者和别名,但是当我尝试应用程序时,我得到了

找不到类“App\Http\Controllers\GooglePlaces”

我的 app.php 在下面,我使用 artisan 发布,并检查了配置文件是否已创建。

我不明白为什么它没有访问下面详述的课程,而是似乎在寻找我自己的课程之一。

'providers' => [

    /*
     * Laravel Framework Service Providers...
     */
    Illuminate\Auth\AuthServiceProvider::class,
    Illuminate\Broadcasting\BroadcastServiceProvider::class,
    Illuminate\Bus\BusServiceProvider::class,
    Illuminate\Cache\CacheServiceProvider::class,
    Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
    Illuminate\Cookie\CookieServiceProvider::class,
    Illuminate\Database\DatabaseServiceProvider::class,
    Illuminate\Encryption\EncryptionServiceProvider::class,
    Illuminate\Filesystem\FilesystemServiceProvider::class,
    Illuminate\Foundation\Providers\FoundationServiceProvider::class,
    Illuminate\Hashing\HashServiceProvider::class,
    Illuminate\Mail\MailServiceProvider::class,
    Illuminate\Pagination\PaginationServiceProvider::class,
    Illuminate\Pipeline\PipelineServiceProvider::class,
    Illuminate\Queue\QueueServiceProvider::class,
    Illuminate\Redis\RedisServiceProvider::class,
    Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
    Illuminate\Session\SessionServiceProvider::class,
    Illuminate\Translation\TranslationServiceProvider::class,
    Illuminate\Validation\ValidationServiceProvider::class,
    Illuminate\View\ViewServiceProvider::class,

    /*
     * Application Service Providers...
     */
    App\Providers\AppServiceProvider::class,
    App\Providers\AuthServiceProvider::class,
    App\Providers\EventServiceProvider::class,
    App\Providers\RouteServiceProvider::class,
    SKAgarwal\GoogleApi\ServiceProvider::class,



],

/*
|--------------------------------------------------------------------------
| Class Aliases
|--------------------------------------------------------------------------
|
| This array of class aliases will be registered when this application
| is started. However, feel free to register as many as you wish as
| the aliases are "lazy" loaded so they don't hinder performance.
|
*/

'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,
    'Cache' => Illuminate\Support\Facades\Cache::class,
    'Config' => Illuminate\Support\Facades\Config::class,
    'Cookie' => Illuminate\Support\Facades\Cookie::class,
    'Crypt' => Illuminate\Support\Facades\Crypt::class,
    'DB' => Illuminate\Support\Facades\DB::class,
    'Eloquent' => Illuminate\Database\Eloquent\Model::class,
    'Event' => Illuminate\Support\Facades\Event::class,
    'File' => Illuminate\Support\Facades\File::class,
    'Gate' => Illuminate\Support\Facades\Gate::class,
    'Hash' => Illuminate\Support\Facades\Hash::class,
    'Lang' => Illuminate\Support\Facades\Lang::class,
    'Log' => Illuminate\Support\Facades\Log::class,
    'Mail' => Illuminate\Support\Facades\Mail::class,
    'Password' => Illuminate\Support\Facades\Password::class,
    'Queue' => Illuminate\Support\Facades\Queue::class,
    'Redirect' => Illuminate\Support\Facades\Redirect::class,
    'Redis' => Illuminate\Support\Facades\Redis::class,
    'Request' => Illuminate\Support\Facades\Request::class,
    'Response' => Illuminate\Support\Facades\Response::class,
    'Route' => Illuminate\Support\Facades\Route::class,
    'Schema' => Illuminate\Support\Facades\Schema::class,
    'Session' => Illuminate\Support\Facades\Session::class,
    'Storage' => Illuminate\Support\Facades\Storage::class,
    'URL' => Illuminate\Support\Facades\URL::class,
    'Validator' => Illuminate\Support\Facades\Validator::class,
    'View' => Illuminate\Support\Facades\View::class,
    'GooglePlaces' => SKAgarwal\GoogleApi\Facade::class,

],

【问题讨论】:

  • 安装包后你运行composer dumpautoload了吗?
  • 错误会说包加载正常...您的控制器没有。

标签: php laravel laravel-5.2


【解决方案1】:

你只是有一个命名空间问题。您需要导入/别名类或通过其完全限定名称引用它:

你可以在你的控制器文件中给它起别名:

namespace App\Http\Controllers;

use GooglePlaces;

class YouController extends ....
{
    public function something()
    {
        GooglePlaces::somemethod();
        ...
     }
 }

或通过其完全限定名称(在本例中您通过 config/app.php 别名)引用它:

public function something()
{
    \GooglePlaces::somemethod();
    ...
}

因为它已通过配置别名为根命名空间“\”。

如果你不给它们取别名或用限定名引用它们,PHP 将在当前命名空间中查找你引用的类,这就是它寻找 App\Http\Controllers\GooglePlaces 的原因,因为你的控制器被声明为在命名空间中App\Http\Controllers.

【讨论】:

  • 完美运行
猜你喜欢
  • 1970-01-01
  • 2015-07-04
  • 2016-08-08
  • 2016-01-08
  • 1970-01-01
  • 2019-02-18
  • 2018-10-03
  • 2013-03-02
  • 2018-06-04
相关资源
最近更新 更多