使用JWT身份验证的laravel 5.7中不存在身份验证控制器

时间:2018-10-28 20:15:11

标签: jwt laravel-5.7

我正在尝试使用laravel 5.7实现JWT身份验证,但是在Laravel中发送请求时遇到了问题。错误如下。

error: Object { message: "Class App\\Http\\Controllers\\App\\Http\\Controllers\\AuthController does not exist", exception: "ReflectionException", file: "C:\\Users\\DELL\\Desktop\\laravel+angular\\backend\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php", … }
​
headers: Object { normalizedNames: Map(0), lazyUpdate: null, lazyInit: lazyInit()
 }
​
message: "Http failure response for http://localhost:8000/api/login: 500 Internal Server Error"
​
name: "HttpErrorResponse"
​
ok: false
​
status: 500
​
statusText: "Internal Server Error"
​
url: "http://localhost:8000/api/login"

这是我的身份验证控制器

   <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;

class AuthController extends Controller
{
    /**
     * Create a new AuthController instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth:api', ['except' => ['login']]);
    }

    /**
     * Get a JWT token via given credentials.
     *
     * @param  \Illuminate\Http\Request  $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function login(Request $request)
    {
        $credentials = $request->only('email', 'password');

        if ($token = $this->guard()->attempt($credentials)) {
            return $this->respondWithToken($token);
        }

        return response()->json(['error' => 'Unauthorized'], 401);
    }

    /**
     * Get the authenticated User
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function me()
    {
        return response()->json($this->guard()->user());
    }

    /**
     * Log the user out (Invalidate the token)
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function logout()
    {
        $this->guard()->logout();

        return response()->json(['message' => 'Successfully logged out']);
    }

    /**
     * Refresh a token.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function refresh()
    {
        return $this->respondWithToken($this->guard()->refresh());
    }

    /**
     * Get the token array structure.
     *
     * @param  string $token
     *
     * @return \Illuminate\Http\JsonResponse
     */
    protected function respondWithToken($token)
    {
        return response()->json([
            'access_token' => $token,
            'token_type' => 'bearer',
            'expires_in' => $this->guard()->factory()->getTTL() * 60,
            'user'=> auth()->user()->name
        ]);
    }

    /**
     * Get the guard to be used during authentication.
     *
     * @return \Illuminate\Contracts\Auth\Guard
     */
    public function guard()
    {
        return Auth::guard();
    }
}

它说AuthController在我检查过的地方不存在,并且在laravel项目的http-> controller文件夹中有它。请帮我 !谢谢.. !!

1 个答案:

答案 0 :(得分:0)

这看起来像一个名称空间问题:

App\Http\Controllers\App\Http\Controllers\AuthController

注意App\Http\Controllers是如何重复的吗?

您似乎已经忘记了前导\,也许在某处使用了App\Http\Controllers\AuthController而不是\App\Http\Controllers\AuthController。如果没有它,则名称空间相对于使用该脚本的任何脚本的当前名称空间都是 relative 。它类似于文件和url中的相对或绝对路径。

如果在定义身份验证路由时发生了这种情况,默认情况下,Laravel会将App\Http\Controllers名称空间放在您为操作指定的控制器名称之前(您可以在RouteServiceProvider.php中看到此名称)。

编辑:

问题似乎是由您遵循的guide中过时的说明引起的:

  

首先让我们在routes/api.php中添加一些路由,如下所示:

Route::group([

    'middleware' => 'api',
    'namespace' => 'App\Http\Controllers',
    'prefix' => 'auth'

], function ($router) {

    Route::post('login', 'AuthController@login');
    Route::post('logout', 'AuthController@logout');
    Route::post('refresh', 'AuthController@refresh');
    Route::post('me', 'AuthController@me');

});

routes/api.php中已经定义的路由已经具有名称空间App\Http\Controllers,因此为此Route::group()提供的名称空间正在被附加到该名称空间,导致我上面提到的重复。 如果删除'namespace' => 'App\Http\Controllers',行,则应解决此特定错误。

您可以在他们的develop branch中看到该行已从指南中删除:

  

首先让我们在routes/api.php中添加一些路由,如下所示:

Route::group([

    'middleware' => 'api',
    'prefix' => 'auth'

], function ($router) {

    Route::post('login', 'AuthController@login');
    Route::post('logout', 'AuthController@logout');
    Route::post('refresh', 'AuthController@refresh');
    Route::post('me', 'AuthController@me');

});