【发布时间】:2017-09-10 14:55:26
【问题描述】:
我查看了 Laravel 中与此错误相关的所有帖子:
Missing argument 1 for Illuminate Support Manager - createDriver()
他们都没有解决我的问题:我使用的是 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->routeMiddleware()调用的注释。 -
@PankitGami 是的,我在引导程序中有 > app.php 这一行: $app->routeMiddleware([ 'auth' => App\Http\Middleware\Authenticate::class, ]);
-
是评论还是未评论?
-
@PankitGami 没有注释,在bootstrap中可以调用routeMiddleware。
标签: laravel laravel-5.4 lumen laravel-middleware