我正在使用dingo/api和nWidart/laravel-modules个包来创建API。
我们知道在 nwidart 包中,每个模块都可以是模型和服务提供商。
现在假设我们有一个PriceList
模型:
class PriceList extends Model
{
protected $primaryKey = 'price_list_id';
protected $fillable = ['title', 'name', 'country', 'rate', 'creator'];
public function __construct(array $attributes = [])
{
$attributes['creator'] = app('Dingo\Api\Auth\Auth')->user()->user_id;
parent::__construct($attributes);
}
}
然后在该模块的PriceListServiceProvider
中,由于某种原因,我需要在配置中获取特定的price list
并存储id
。为此,我写了这些:
class PriceListServiceProvider extends ServiceProvider
{
protected $defer = FALSE;
public function boot()
{
$this->registerTranslations();
$this->registerConfig();
$this->registerViews();
$this->registerFactories();
$this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations');
$priceList =
PriceList::where('rate', 'toman')->orderBy('created_at', 'asc')->first();
if ($priceList) {
$defaultPriceList = $priceList->price_list_id;
}
config('pricelist.default',$defaultPriceList);
}
}
但在运行项目后,我每次都会收到此错误:
Symfony\Component\Debug\Exception\FatalThrowableError: Type error: Argument 2 passed to Dingo\Api\Auth\Provider\JWT::authenticate() must be an instance of Dingo\Api\Routing\Route, null given, called in D:\wamp\www\zarsam-app\vendor\dingo\api\src\Auth\Auth.php on line 82 in file D:\wamp\www\zarsam-app\vendor\dingo\api\src\Auth\Provider\JWT.php on line 41
Stack trace:
1. Symfony\Component\Debug\Exception\FatalThrowableError->() D:\wamp\www\zarsam-app\vendor\dingo\api\src\Auth\Provider\JWT.php:41
2. Dingo\Api\Auth\Provider\JWT->authenticate() D:\wamp\www\zarsam-app\vendor\dingo\api\src\Auth\Auth.php:82
3. Dingo\Api\Auth\Auth->authenticate() D:\wamp\www\zarsam-app\vendor\dingo\api\src\Auth\Auth.php:151
4. Dingo\Api\Auth\Auth->getUser() D:\wamp\www\zarsam-app\vendor\dingo\api\src\Auth\Auth.php:166
5. Dingo\Api\Auth\Auth->user() D:\wamp\www\zarsam-app\Modules\PriceList\Entities\PriceList.php:18
6. Modules\PriceList\Entities\PriceList->__construct() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1450
7. Illuminate\Database\Eloquent\Model->__callStatic() D:\wamp\www\zarsam-app\app\Providers\AppServiceProvider.php:37
8. App\Providers\AppServiceProvider->boot() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:29
9. call_user_func_array() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:29
10. Illuminate\Container\BoundMethod->Illuminate\Container\{closure}() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:87
11. Illuminate\Container\BoundMethod->callBoundMethod() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:31
12. Illuminate\Container\BoundMethod->call() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Container\Container.php:549
13. Illuminate\Container\Container->call() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:792
14. Illuminate\Foundation\Application->bootProvider() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:775
15. Illuminate\Foundation\Application->Illuminate\Foundation\{closure}() [internal]:0
16. array_walk() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:776
17. Illuminate\Foundation\Application->boot() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Foundation\Bootstrap\BootProviders.php:17
18. Illuminate\Foundation\Bootstrap\BootProviders->bootstrap() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:213
19. Illuminate\Foundation\Application->bootstrapWith() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:162
20. Illuminate\Foundation\Http\Kernel->bootstrap() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:146
21. Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:116
22. Illuminate\Foundation\Http\Kernel->handle() D:\wamp\www\zarsam-app\public\index.php:55
如果删除这些行,则不会发生错误。
即使我将代码移到主AppServiceProvider
,但又发生了同样的错误。