简体   繁体   English

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

[英]ERROR Class app\Auth not found - slim framework v3 middleware

I am using slim framework and trying to implement slim token authentication as middleware, now whenever i go to 我正在使用slim框架并尝试将slim token身份验证作为中间件实现,现在每当我去的时候

localhost/project/restrict 本地主机/项目/限制

i get the message "Token Not Found" which seems to be working fine however when i try to pass the token in the authorization parameter as per the middleware documentation 我收到消息“令牌未找到”这似乎工作正常但是当我尝试按照中间件文档传递授权参数中的令牌

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

i always get the error Class 'app\\Auth' not found and in my error trace the below, 我总是得到错误类'app \\ Auth'未找到,并在我的错误跟踪下面,

0 /Applications/AMPPS/www/project/vendor/dyorg/slim-token-authentication/src/TokenAuthentication.php(66): {closure}(Object(Slim\\Http\\Request), Object(Slim\\Middleware\\TokenAuthentication)) 0 /Applications/AMPPS/www/project/vendor/dyorg/slim-token-authentication/src/TokenAuthentication.php(66):{closure}(Object(Slim \\ Http \\ Request),Object(Slim \\ Middleware \\ TokenAuthentication) )

1 [internal function]: Slim\\Middleware\\TokenAuthentication->__invoke(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response), Object(Slim\\App)) 1 [内部函数]:Slim \\ Middleware \\ TokenAuthentication - > __ invoke(Object(Slim \\ Http \\ Request),Object(Slim \\ Http \\ Response),Object(Slim \\ App))

2 /Applications/AMPPS/www/project/vendor/slim/slim/Slim/DeferredCallable.php(43): call_user_func_array(Object(Slim\\Middleware\\TokenAuthentication), Array) 2 /Applications/AMPPS/www/project/vendor/slim/slim/Slim/DeferredCallable.php(43):call_user_func_array(Object(Slim \\ Middleware \\ TokenAuthentication),Array)

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

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

5 /Applications/AMPPS/www/project/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(122): Slim\\App->Slim{closure}(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response)) 5 /Applications/AMPPS/www/project/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(122):Slim \\ App-> Slim {closure}(Object(Slim \\ Http \\ Request),Object(Slim \\ Http \\响应))

6 /Applications/AMPPS/www/project/vendor/slim/slim/Slim/App.php(370): Slim\\App->callMiddlewareStack(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response)) 6 /Applications/AMPPS/www/project/vendor/slim/slim/Slim/App.php(370):Slim \\ App-> callMiddlewareStack(Object(Slim \\ Http \\ Request),Object(Slim \\ Http \\ Response))

7 /Applications/AMPPS/www/project/vendor/slim/slim/Slim/App.php(295): Slim\\App->process(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response)) 7 /Applications/AMPPS/www/project/vendor/slim/slim/Slim/App.php(295):Slim \\ App-> process(Object(Slim \\ Http \\ Request),Object(Slim \\ Http \\ Response))

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

9 {main} 9 {主}

here the code i am using 这里是我正在使用的代码

<?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();

?>

The reason that \\app\\Auth cannot be found is because it doesn't exist in the current composer autoload path. 无法找到\\app\\Auth的原因是因为它在当前作曲家自动加载路径中不存在。

First move the app to the root folder, where core and the root vendor folders are. 首先将app移动到根文件夹,其中包含core和根vendor文件夹。

Then add 然后加

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

to the root composer.json. 到根composer.json。

Last, run composer dump-autoload -o in the root folder. 最后,在根文件夹中运行composer dump-autoload -o

After that, \\app\\Auth should be in the autoload path and everything should work as expected. 在那之后, \\app\\Auth应该在自动加载路径中,一切都应该按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM