我正在使用slim框架并试图实现slim token authentication作为中间件,现在每当我去
localhost / project / restrict
我收到消息"Token Not Found"似乎工作正常但是当我尝试按照中间件documentation在授权参数中传递令牌时
locahost / project / restrict?authorization = usertokensecret
我总是得到错误类'app \ Auth'未找到,并在我的错误跟踪下面,
0 /Applications/AMPPS/www/project/vendor/dyorg/slim-token-authentication/src/TokenAuthentication.php(66):(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),Object(Slim \ App))5 /Applications/AMPPS/www/project/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(122) ):Slim \ App-> Slim (Object(Slim \ Http \ Request),Object(Slim \ Http \ Response))6 / Application s / 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
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();
?>
1 回答
无法找到
\app\Auth
的原因是因为它在当前编写器自动加载路径中不存在 .首先将
app
移动到根文件夹,其中core
和根vendor
文件夹是 .然后加
到根composer.json .
最后,在根文件夹中运行
composer dump-autoload -o
.在那之后,
\app\Auth
应该在自动加载路径中,一切都应该按预期工作 .