ERROR Class app \ Auth not found - slim framework v3中间件

时间:2017-04-22 10:07:29

标签: php authentication slim

我正在使用slim框架并试图实现slim token authentication作为中间件,现在每当我去

  

本地主机/项目/限制

我收到消息"令牌未找到"这似乎工作正常但是当我尝试按照中间件documentation

在授权参数中传递令牌时
  

的locahost /项目/限制?授权= usertokensecret

我总是得到错误类' app \ Auth'找不到,并在我的错误跟踪下面,

  

0 /Applications/AMPPS/www/project/vendor/dyorg/slim-token-authentication/src/TokenAuthentication.php(66):   {闭合}(对象(超薄\ HTTP \请求),   对象(超薄\中间件\ TokenAuthentication))

     

1 [内部功能]:Slim \ Middleware \ TokenAuthentication-> __ invoke(Object(Slim \ Http \ Request),   对象(Slim \ Http \ Response),Object(Slim \ App))

     

2 /Applications/AMPPS/www/project/vendor/slim/slim/Slim/DeferredCallable.php(43):   call_user_func_array(对象(超薄\中间件\ TokenAuthentication)   阵列)

     

3 [内部函数]:Slim \ DeferredCallable-> __ invoke(Object(Slim \ Http \ Request),   对象(Slim \ Http \ Response),Object(Slim \ App))

     

4 /Applications/AMPPS/www/project/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(73):   call_user_func(对象(超薄\ DeferredCallable)   对象(Slim \ Http \ Request),Object(Slim \ Http \ Response),   对象(超薄\ APP))

     

5 /Applications/AMPPS/www/project/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(122):   超薄\ APP->纤细{闭合}(对象(超薄\ HTTP \请求),   对象(超薄\ HTTP \响应))

     

6 /Applications/AMPPS/www/project/vendor/slim/slim/Slim/App.php(370):Slim \ App-> callMiddlewareStack(Object(Slim \ Http \ Request),   对象(超薄\ HTTP \响应))

     

7 /Applications/AMPPS/www/project/vendor/slim/slim/Slim/App.php(295):Slim \ App->进程(对象(Slim \ Http \ Request),   对象(超薄\ HTTP \响应))

     

8 /Applications/AMPPS/www/project/index.php(81):Slim \ App-> run()

     

9 {main}

这里是我正在使用的代码

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require_once './vendor/autoload.php';

$app = new \Slim\App;
use Slim\App;
use Slim\Middleware\TokenAuthentication;

$config = [
    'settings' => [
        'displayErrorDetails' => true
    ]
];

$app = new App($config);

$authenticator = function($request, TokenAuthentication $tokenAuth){

    $token = $tokenAuth->findToken($request);
    $auth = new \app\Auth();
    $auth->getUserByToken($token);

};

/**
 * Add token authentication middleware
 */
$app->add(new TokenAuthentication([
    'path' =>   '/restrict',
    'authenticator' => $authenticator
]));

/**
 * Public route example
 */
$app->get('/', function($request, $response){
    $output = ['msg' => 'It is a public area'];
    $response->withJson($output, 200, JSON_PRETTY_PRINT);
});


/**
 * Restrict route example
 * Our token is "usertokensecret"
 */
$app->get('/restrict', function($request, $response){
    $output = ['msg' => 'It\'s a restrict area. Token authentication works!'];
    $response->withJson($output, 200, JSON_PRETTY_PRINT);
});


$app->run();

?>

1 个答案:

答案 0 :(得分:0)

无法找到\app\Auth的原因是因为它在当前的编辑器自动加载路径中不存在。

首先将app移至根文件夹,其中core和根vendor文件夹为。

然后添加

"autoload": {
    "classmap": [
      "app"
    ]
}

到根composer.json。

最后,在根文件夹中运行composer dump-autoload -o

之后,\app\Auth应该在自动加载路径中,一切都应该按预期工作。