我需要的是:使用自定义类来接收 HTTP 请求并处理它。
我目前拥有的:
$app->group('/user', function () use ($app) {
$app->post('/login', Auth::class . ':login');
}
在我的授权类中:
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
// also tried: use Slim\Http\Request; use Slim\Http\Response;
class Auth {
protected $container;
public function __construct(&$c) {
$this->container = $c;
}
public function login(Request $request, Response $response, $args) {
// NEED TO GET GET/POST/PUT Params here
// When I Try to print $request, $response or $args, it returns empty
}
我的代码在函数登录中显示了我需要的东西:获取 http 参数和请求/响应/args 参数。 (我想在一个类中实现很多功能,我的意思是,我不想使用 __invoke()) 我不明白的另一件事是,如果我做类似的事情 返回 $response->withJson($somearraydata,200); 变量 $response 实际上有效。为什么?
感谢您的帮助!
最佳答案
我想我已经明白了,
$args 永远不会设置(除非它是一个获取),但请求和响应是。
并且,为了获得参数,我可以这样做: $request->getParsedBody()['属性']
希望这对某人有帮助。 :)
关于php - 超薄 Controller 请求、响应和参数不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43987971/