超薄控制器请求、响应和参数不可用
Slim Controller Request, response and args not available
我需要的是:使用自定义 class 接收 HTTP 请求并处理它。
我目前拥有的:
$app->group('/user', function () use ($app) {
$app->post('/login', Auth::class . ':login');
}
在我的授权中 Class:
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 参数和 request/response/args 参数。 (我想在一个class中实现很多功能,我的意思是,我不想使用__invoke())
我不明白的另一件事是,如果我做类似的事情
return $response->withJson($somearraydata,200);
变量 $response 实际上有效。为什么?
感谢您的帮助!
我想我已经弄明白了,
$args 永远不会设置(除非它是一个获取),但请求和响应是。
而且,为了获得参数,我可以这样做:
$request->getParsedBody()['attribute']
希望这对某人有所帮助。 :)
我需要的是:使用自定义 class 接收 HTTP 请求并处理它。
我目前拥有的:
$app->group('/user', function () use ($app) {
$app->post('/login', Auth::class . ':login');
}
在我的授权中 Class:
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 参数和 request/response/args 参数。 (我想在一个class中实现很多功能,我的意思是,我不想使用__invoke()) 我不明白的另一件事是,如果我做类似的事情 return $response->withJson($somearraydata,200); 变量 $response 实际上有效。为什么?
感谢您的帮助!
我想我已经弄明白了,
$args 永远不会设置(除非它是一个获取),但请求和响应是。
而且,为了获得参数,我可以这样做: $request->getParsedBody()['attribute']
希望这对某人有所帮助。 :)