错误 Class app\Auth 未找到 - slim 框架 v3 中间件
ERROR Class app\Auth not found - slim framework v3 middleware
我正在使用 slim 框架并尝试将 slim token authentication 实现为中间件,现在每当我去
localhost/project/restrict
我收到消息“未找到令牌”,这似乎工作正常但是当我尝试根据中间件在授权参数中传递令牌时 documentation
locahost/project/restrict?authorization=usertokensecret
我总是收到错误 Class 'app\Auth' not found 并且在我的错误跟踪下面,
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))
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))
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{closure}(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))
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();
?>
找不到\app\Auth
的原因是因为它不存在于当前的composer自动加载路径中。
首先将 app
移动到根文件夹,其中 core
和根 vendor
文件夹。
然后添加
"autoload": {
"classmap": [
"app"
]
}
到根 composer.json.
最后,运行 composer dump-autoload -o
在根文件夹中。
在那之后,\app\Auth
应该在自动加载路径中,一切都应该按预期工作。
我正在使用 slim 框架并尝试将 slim token authentication 实现为中间件,现在每当我去
localhost/project/restrict
我收到消息“未找到令牌”,这似乎工作正常但是当我尝试根据中间件在授权参数中传递令牌时 documentation
locahost/project/restrict?authorization=usertokensecret
我总是收到错误 Class 'app\Auth' not found 并且在我的错误跟踪下面,
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))
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))
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{closure}(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))
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();
?>
找不到\app\Auth
的原因是因为它不存在于当前的composer自动加载路径中。
首先将 app
移动到根文件夹,其中 core
和根 vendor
文件夹。
然后添加
"autoload": {
"classmap": [
"app"
]
}
到根 composer.json.
最后,运行 composer dump-autoload -o
在根文件夹中。
在那之后,\app\Auth
应该在自动加载路径中,一切都应该按预期工作。