我有在依賴注入的問題,我得到的錯誤是:爲什麼依賴注入不起作用?
local.ERROR: exception 'ErrorException' with message 'Argument 1 passed to App\Http\Controllers\APIsController::get_JWT_token() must be an instance of GenTux\Jwt\JwtToken, integer given, called in C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\app\Services\Access\Traits\RegistersUsers.php on line 82 and defined' in C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\app\Http\Controllers\APIsController.php:163
雖然我包括JwtToken在APIsController爲:
use GenTux\Jwt\GetsJwtToken;
use GenTux\Jwt\JwtToken;
use GetsJwtToken;
// $jwt is not injected
public function get_JWT_token(JwtToken $jwt, $user_id = null)
{
if ($user_id == null) {
$payload = [
'exp' => time() + (60 * 60 * 24 * 30),
'user_id' => Auth::user()->getKey(),
'product_name' => env('PRODUCT_NAME')
];
} else {
$payload = [
'exp' => time() + (60 * 60 * 24 * 360),
'user_id' => $user_id,
'product_name' => env('PRODUCT_NAME')
];
}
$token = 'Bearer ' . $jwt->createToken($payload); // new instance of JwtToken
return $token;
}
我做的調用方法爲:
$apis = new APIsController();
$JWT_token = $apis->get_JWT_token($user->id);
我還註冊包爲: [配置\ app.php]: 在提供商:
GenTux\Jwt\Support\LaravelServiceProvider::class,
那麼,爲什麼依賴注入不起作用?
以下是完整的堆棧跟蹤:
#0 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\app\Http\Controllers\APIsController.php(163): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(4096, 'Argument 1 pass...', 'C:\\Bitnami\\wapp...', 163, Array)
#1 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\app\Services\Access\Traits\RegistersUsers.php(82): App\Http\Controllers\APIsController->get_JWT_token(Object(App\Models\Access\User\User))
#2 [internal function]: App\Http\Controllers\Frontend\Auth\AuthController->register(Object(App\Http\Requests\Frontend\Auth\RegisterRequest))
#3 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Routing\Controller.php(80): call_user_func_array(Array, Array)
#4 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(146): Illuminate\Routing\Controller->callAction('register', Array)
#5 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(94): Illuminate\Routing\ControllerDispatcher->call(Object(App\Http\Controllers\Frontend\Auth\AuthController), Object(Illuminate\Routing\Route), 'register')
#6 [internal function]: Illuminate\Routing\ControllerDispatcher->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#7 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#8 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#9 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#10 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(96): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#11 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(54): Illuminate\Routing\ControllerDispatcher->callWithinStack(Object(App\Http\Controllers\Frontend\Auth\AuthController), Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'register')
#12 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Routing\Route.php(174): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'App\\Http\\Contro...', 'register')
#13 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Routing\Route.php(140): Illuminate\Routing\Route->runController(Object(Illuminate\Http\Request))
#14 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Routing\Router.php(724): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#15 [internal function]: Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#16 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#17 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Routing\Middleware\ThrottleRequests.php(48): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#18 [internal function]: Illuminate\Routing\Middleware\ThrottleRequests->handle(Object(Illuminate\Http\Request), Object(Closure), '60', '1')
#19 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(136): call_user_func_array(Array, Array)
#20 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#21 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#22 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\app\Http\Middleware\RedirectIfAuthenticated.php(28): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#23 [internal function]: App\Http\Middleware\RedirectIfAuthenticated->handle(Object(Illuminate\Http\Request), Object(Closure))
#24 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(136): call_user_func_array(Array, Array)
#25 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#26 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#27 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\app\Http\Middleware\LocaleMiddleware.php(48): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#28 [internal function]: App\Http\Middleware\LocaleMiddleware->handle(Object(Illuminate\Http\Request), Object(Closure))
#29 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(136): call_user_func_array(Array, Array)
#30 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#31 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#32 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\app\Http\Middleware\FilterIfTurbolinks.php(18): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#33 [internal function]: App\Http\Middleware\FilterIfTurbolinks->handle(Object(Illuminate\Http\Request), Object(Closure))
#34 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(136): call_user_func_array(Array, Array)
#35 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#36 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#37 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\View\Middleware\ShareErrorsFromSession.php(49): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#38 [internal function]: Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#39 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(136): call_user_func_array(Array, Array)
#40 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#41 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#42 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php(64): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#43 [internal function]: Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#44 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(136): call_user_func_array(Array, Array)
#45 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#46 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#47 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse.php(37): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#48 [internal function]: Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#49 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(136): call_user_func_array(Array, Array)
#50 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#51 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#52 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\EncryptCookies.php(59): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#53 [internal function]: Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#54 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(136): call_user_func_array(Array, Array)
#55 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#56 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#57 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#58 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#59 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Routing\Router.php(726): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#60 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Routing\Router.php(699): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#61 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Routing\Router.php(675): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#62 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(246): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#63 [internal function]: Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#64 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#65 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\barryvdh\laravel-debugbar\src\Middleware\Debugbar.php(51): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#66 [internal function]: Barryvdh\Debugbar\Middleware\Debugbar->handle(Object(Illuminate\Http\Request), Object(Closure))
#67 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(136): call_user_func_array(Array, Array)
#68 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#69 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#70 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php(44): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#71 [internal function]: Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#72 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(136): call_user_func_array(Array, Array)
#73 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#74 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#75 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#76 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#77 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(132): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#78 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(99): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#79 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\public\index.php(54): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#80 C:\Bitnami\wappstack-5.6.29-0\apache2\htdocs\testproject\business-web\server.php(21): require_once('C:\\Bitnami\\wapp...')
#81 {main}
我明白,但我無法實例化對象。這些是需要的參數,但我不確定在實例化對象時要爲每個參數放置什麼。公共職能__construct(JwtDriverInterface $智威湯遜,$祕密= NULL,$算法= NULL) – WingsOfAltair
@ user7409342我修改我的回答與你輸入 – Gayan
後,我編輯,以匹配你貼什麼方法我收到此錯誤。錯誤:使用未定義的常量JwtToken - 假定爲'JwtToken'。我們離先生們又近了一步! – WingsOfAltair