【问题标题】:Missing argument 1 for Illuminate\\Support\\Manager::createDriver()Illuminate\\Support\\Manager::createDriver() 缺少参数 1
【发布时间】:2017-09-10 14:55:26
【问题描述】:

我查看了 Laravel 中与此错误相关的所有帖子:

Missing argument 1 for Illuminate Support Manager - createDriver()

2th link

3th link

他们都没有解决我的问题:我使用的是 Laravel Lumen 5.4 版和Dingo API package

我想访问传入请求中的 Authenticated User:

 $request->user(); //returns an instance of the authenticated user

但是这会给我一个错误提示:

缺少 Illuminate\Support\Manager::createDriver() 的参数 1,在第 88 行的 /var/www/html/myApp/vendor/illuminate/support/Manager.php 中调用并定义”,

我知道为了获取Authenticated User,需要在路由里面提供Auth Middleware:

  $api->get('register/{accountId}', ['middleware' => 'auth', 'App\Http\Controllers\Api\V1\RegisterController@registerAction']);

但是在我的路由中添加中间件身份验证实际上不会到达控制器端点并抛出与上面相同的错误。

我在 bootstrap/app.php 中注册了 AuthServiceProvider:

 $app->register(App\Providers\AuthServiceProvider::class);

这是 AuthServiceProvider 类:

 <?php

 namespace App\Providers;

 use App\Models\Account;
 use Illuminate\Support\ServiceProvider;

 class AuthServiceProvider extends ServiceProvider {
  /**
   * Register any application services.
   *
   * @return void
   */
   public function register() {
   }

  /**
   * Boot the authentication services for the application.
   *
   * @return void
   */
   public function boot() {
    // Here you may define how you wish users to be authenticated for your Lumen
    // application. The callback which receives the incoming request instance
    // should return either a User instance or null. You're free to obtain
    // the User instance via an API token or any other method necessary.

    $this->app['auth']->viaRequest('api', function ($request) {

        if ($request->input('api_token')) {
            return Account::where('api_token', $request->input('api_token'))->first();
        }
    });
   }
  }

这是我在 config/auth.php 中的内容:

  <?php

  return [

/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|
*/

'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],

/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session", "token"
|
*/

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],
],

/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
],

/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| You may specify multiple password reset configurations if you have more
| than one user table or model in the application and you want to have
| separate password reset settings based on the specific user types.
|
| The expire time is the number of minutes that the reset token should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/

'passwords' => [
    'users' => [
        'provider' => 'users',
        'table' => 'password_resets',
        'expire' => 60,
    ],
 ],

];

我尝试自己调试这个问题,我发现这是 Laravel 中出现问题的地方:

/**
 * Create a new driver instance.
 *
 * @param  string  $driver
 * @return mixed
 *
 * @throws \InvalidArgumentException
 */
protected function createDriver($driver)
{
    // We'll check to see if a creator method exists for the given driver. If not we
    // will check for a custom driver creator, which allows developers to create
    // drivers using their own customized driver creator Closure to create it.
    if (isset($this->customCreators[$driver])) {
        return $this->callCustomCreator($driver);
    } else {
        $method = 'create'.Str::studly($driver).'Driver';

        if (method_exists($this, $method)) {
            return $this->$method();
        }
    }
    throw new InvalidArgumentException("Driver [$driver] not supported.");
}

我可以在 stackTrace 中看到它在 createDriver() 方法中传递了一个 NULL 驱动程序,这导致了我遇到的错误。我想知道这不是我必须在我的 .env 文件中添加的简单配置。

我从 Laravel Lumen 开始,它是构建 API 的好工具,我并没有责怪工具本身(我花了很多时间阅读漂亮的文档),我很确定我错过了一些非常简单的东西,如果有人能指导我,我会很高兴的。

【问题讨论】:

  • 您是否取消了对bootstrap/app.php 中的$app-&gt;routeMiddleware() 调用的注释。
  • @PankitGami 是的,我在引导程序中有 > app.php 这一行: $app->routeMiddleware([ 'auth' => App\Http\Middleware\Authenticate::class, ]);
  • 是评论还是未评论?
  • @PankitGami 没有注释,在bootstrap中可以调用routeMiddleware。

标签: laravel laravel-5.4 lumen laravel-middleware


【解决方案1】:

您是否使用Laravel Scout(带有Algolia 搜索驱动程序)?

我在运行我的数据库种子时遇到了您看到的错误。最后,我意识到我的疲惫的头脑忘记在我的 .env 文件中定义正确的 env 变量,如下所示:

SCOUT_DRIVER=algolia
ALGOLIA_APP_ID=yourAlgoliaAppId
ALGOLIA_SECRET=yourAlgoliaSecret

如果您尚未发布 scout 配置文件,请使用以下命令:

php工匠供应商:发布 --provider="Laravel\Scout\ScoutServiceProvider"

在运行上述命令之前,请确保您已将 'Laravel\Scout\ScoutServiceProvider::class' 放入 'config/app.php' 的 providers 数组中。

一旦我发布了配置文件(默认命名为 config/scout.php),我使用了如下环境变量:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Search Engine
    |--------------------------------------------------------------------------
    |
    | This option controls the default search connection that gets used while
    | using Laravel Scout. This connection is used when syncing all models
    | to the search service. You should adjust this based on your needs.
    |
    | Supported: "algolia", "elasticsearch", "null"
    |
    */

    'driver' => env('SCOUT_DRIVER'),

    /*
    |--------------------------------------------------------------------------
    | Index Prefix
    |--------------------------------------------------------------------------
    |
    | Here you may specify a prefix that will be applied to all search index
    | names used by Scout. This prefix may be useful if you have multiple
    | "tenants" or applications sharing the same search infrastructure.
    |
    */

    'prefix' => env('SCOUT_PREFIX', ''),

    /*
    |--------------------------------------------------------------------------
    | Queue Data Syncing
    |--------------------------------------------------------------------------
    |
    | This option allows you to control if the operations that sync your data
    | with your search engines are queued. When this is set to "true" then
    | all automatic data syncing will get queued for better performance.
    |
    */

    'queue' => false,

    /*
    |--------------------------------------------------------------------------
    | Algolia Configuration
    |--------------------------------------------------------------------------
    |
    | Here you may configure your Algolia settings. Algolia is a cloud hosted
    | search engine which works great with Scout out of the box. Just plug
    | in your application ID and admin API key to get started searching.
    |
    */

    'algolia' => [
        'id' => env('ALGOLIA_APP_ID'),
        'secret' => env('ALGOLIA_SECRET'),
    ],

];

完成上述所有操作后,错误就消失了。此答案可能无法直接解决您的确切问题,但可能会给您一些想法或帮助您进行故障排除。

【讨论】:

    猜你喜欢
    • 2016-02-11
    • 2018-01-07
    • 2018-06-26
    • 2016-01-01
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 2017-10-05
    相关资源
    最近更新 更多