为什么依赖注入不起作用?

时间:2017-01-12 11:40:27

标签: php laravel laravel-5.2

我在依赖注入方面遇到问题,我得到的错误是:

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

虽然我在APIController中包含JwtToken:

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}

2 个答案:

答案 0 :(得分:1)

错误是

Argument 1 passed to App\Http\Controllers\APIsController::get_JWT_token() must be an instance of GenTux\Jwt\JwtToken, integer given

在这里

$JWT_token = $apis->get_JWT_token($user->id);

你确实给出了一个整数作为第一个参数,即$user->id

相反,你应该提供

GenTux\Jwt\JwtToken

实例。

- 在@ user7409342的输入后编辑 -

你可以这样做

public function get_JWT_token($user_id = null)
{
    $jwt = app(JwtToken::class); // Resolve JwtToken using app()
    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;
}

答案 1 :(得分:0)

$ JWT_token = $ apis-> get_JWT_token($ user-> id);

这里你传递第一个参数为user_id和get_JWT_token函数第一个参数是JwtToken对象,它正在产生问题所以改变函数的定义,如下所示。

public function get_JWT_token(JwtToken $ jwt,$ user_id = null) { ... //你的代码 }