流明认证错误 - Container.php第572行中的ReflectionException:类请求不存在

时间:2016-10-08 08:50:35

标签: php laravel lumen

我收到错误:

  

Container.php第572行中的ReflectionException:类请求没有   存在

请参阅我用于 routes.php 页面的代码。

$app->get('/records', ['middleware' => 'auth', function (Request $request) {
    $user = Auth::user();
    return json_encode($user);
}]);

我已按照Lumen网站上的文档进行操作。使用 bootstrap / app.php

取消注释相关行

以下是我的中间件代码:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Factory as Auth;

class Authenticate
{
    protected $auth;
    public function __construct(Auth $auth)
    {
        $this->auth = $auth;
    }

    public function handle($request, Closure $next, $guard = null)
    {
        if ($this->auth->guard($guard)->guest()) {
            return response()->json('unauthorized', 401);
        }

        return $next($request);
    }
}

和AuthServiceProvider文件:

<?php

namespace App\Providers;

use App\User;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    public function register()
    {
    }

    public function boot()
    {
        $this->app['auth']->viaRequest('api', function ($request) {
            if ($request->header('Authorization')) {
                $user = User:: where('api_token', '=', substr($request->header('Authorization'), 6))->first();
                return $user;
            }
            return null;
        });
    }
}

1 个答案:

答案 0 :(得分:1)

添加此行

use Illuminate\Http\Request; 
你的routes.php中的