簡體   English   中英

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

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

我正在使用slim框架並嘗試將slim token身份驗證作為中間件實現,現在每當我去的時候

本地主機/項目/限制

我收到消息“令牌未找到”這似乎工作正常但是當我嘗試按照中間件文檔傳遞授權參數中的令牌

的locahost /項目/限制?授權= usertokensecret

我總是得到錯誤類'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) )

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)

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) ),對象(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 \\響應))

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))

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

9 {主}

這里是我正在使用的代碼

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

?>

無法找到\\app\\Auth的原因是因為它在當前作曲家自動加載路徑中不存在。

首先將app移動到根文件夾,其中包含core和根vendor文件夾。

然后加

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

到根composer.json。

最后,在根文件夾中運行composer dump-autoload -o

在那之后, \\app\\Auth應該在自動加載路徑中,一切都應該按預期工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM