Laravel 5.1 App\Http\Controllers\JWT class 未找到

Laravel 5.1 App\Http\Controllers\JWT class not found

我正在使用 JWT 在我的 angular - laravel 应用程序中进行身份验证。一切正常,除了需要生成令牌时,出现错误

App\Http\Controllers\JWT

我尝试在 AuthController 中添加以下行:-

use Tymon\JWTAuth\Providers\JWTAuthServiceProvider;

use App\Http\Controllers\JWT;

use JWT;

但似乎没有任何效果

我的composer.json:-

"require": {
    "php": ">=5.5.9",
    "laravel/framework": "5.1.*",
    "tymon/jwt-auth": "0.5.*"
},

AuthController -

$payload = [
        'sub' => $user->id,
        'iat' => time(),
        'exp' => time() + (2 * 7 * 24 * 60 * 60)
    ];
    return JWT::encode($payload, Config::get('app.token_secret'));

public function signup(Request $request){
    $user = new User;
    $user->name = $request->input('displayName');
    $user->email = $request->input('email');
    /*$user->password = Hash::make($request->input('password'));*/
    $user->password = $request->input('password');
    $user->save();
    return response()->json(['token' => $this->createToken($user)]);
}

这应该只是一个命名空间问题。我想你想要 JWTAuth 门面。

use JWTAuth;  // if added to aliases as the install mentions
use Tymon\JWTAuth\Facades\JWTAuth; // if not aliased

更新: 如果您没有更改 class 中的代码,这意味着您仍然需要调用 JWT,您可以为这些导入设置别名,而不必更改对 JWT 的调用。

use JWTAuth as JWT;
use Tymon\JWTAuth\Facades\JWTAuth as JWT;

试试,

JWTAuth;

然而,JWTAuth::encode不存在,如果你必须使用encode方法那么试试这个,

use Tymon\JWTAuth\JWTManager as JWT;

然后就可以访问encode方法

$customClaims = [
    'sub' => $user->id,
    'iat' => time(),
    'exp' => time() + (2 * 7 * 24 * 60 * 60)
];

$payload = app('tymon.jwt.payload.factory')->make($customClaims);

return JWT::encode($payload, Config::get('app.token_secret'));