未找到Laravel Passport安装类

时间:2017-06-27 18:31:08

标签: php laravel-5 laravel-passport

我正在尝试将我的Laravel应用配置为使用Passport,并且在我的AppServiceProvider.php中加载该类时遇到困难。这就是我所做的......

<?php // AuthServiceProvider.php

namespace S1\Providers;

use Laravel\Passport\PassportServiceProvider;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as 
ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
...
    public function boot()
    {
        $this->registerPolicies();
        Passport::routes();
        Passport::tokensCan([
            'client' => 'this is a api client test'
        ]);
    }
}

<?php .  // auth.php
 ...
return [

    'guards' => [
        'web' => [
             'driver' => 'passport',
            'provider' => 'users',
        ],
], ...

<?php // app.php

'providers' => [
...
Laravel\Passport\PassportServiceProvider::class,
....

<?php  // AppServiceProvider.php

namespace S1\Providers;

use Illuminate\Support\ServiceProvider;
use S1\Providers\AppServiceProvider;
use Laravel\Passport\PassportServiceProvider;

class AppServiceProvider extends ServiceProvider
{

    public function register()
    {
        // Enable Laravel Migrations Generator for development only
        if ($this->app->environment() !== 'production') {
            $this->app->register(\Way\Generators\GeneratorsServiceProvider::class);
            $this->app->register(\Xethron\MigrationsGenerator\MigrationsGeneratorServiceProvider::class);
        }
    }
}

结果是收到以下错误:

  [Symfony\Component\Debug\Exception\FatalThrowableError]  
  Class 'S1\Providers\Passport' not found   

我确实有这个工作,但不得不重建,现在我无法通过这个错误。我的命名空间正在发生一些事情,但我无法弄清楚是什么。

4 个答案:

答案 0 :(得分:8)

好像您忘了导入护照,请尝试添加此

use App\Passport\Passport;

到您的AppServiceProvider

<?php  // AppServiceProvider.php

namespace S1\Providers;

use App\Passport\Passport;

use Illuminate\Support\ServiceProvider;
use S1\Providers\AppServiceProvider;
use Laravel\Passport\PassportServiceProvider;

class AppServiceProvider extends ServiceProvider
{

    public function register()
    {
        // Enable Laravel Migrations Generator for development only
        if ($this->app->environment() !== 'production') {
            $this->app->register(\Way\Generators\GeneratorsServiceProvider::class);
            $this->app->register(\Xethron\MigrationsGenerator\MigrationsGeneratorServiceProvider::class);
        }
    }
}

答案 1 :(得分:2)

请检查您的 composer.json ,是否将laravel / passport包含在“require”中,如下所示:

"require": {
        "php": ">=5.6.4",
        "laravel/framework": "5.4.*",
        "laravel/passport": "^3.0",
        "laravel/tinker": "~1.0"
 },

如果不重新运行

  

作曲家需要laravel /护照

答案 2 :(得分:1)

为您的特定Passport版本安装正确的Laravel版本。假设你有Laravel 5.5,安装Passport 4.0:

composer require laravel/passport=~4.0

检查您的Laravel版本:

php artisan --version

或查看"laravel/framework": "5.5.*",中的require对象下的composer.json

答案 3 :(得分:0)

命名空间App \ Providers;

使用Laravel \ Passport \ Passport;

使用Illuminate \ Support \ Facades \ Gate; 使用Laravel \ Passport \ PassportServiceProvider; 使用Illuminate \ Foundation \ Support \ Providers \ AuthServiceProvider作为ServiceProvider;

类AuthServiceProvider扩展了ServiceProvider {     / **      *应用程序的策略映射。      *      * @var数组      * /     受保护的$ policies = [         &#39;应用软件\模型&#39; =&GT; &#39;应用\策略\ ModelPolicy&#39 ;,     ];

/**
 * Register any authentication / authorization services.
 *
 * @return void
 */
public function boot()
{
    $this->registerPolicies();

    Passport::routes();

    //
}

}