Search code examples
phpslimslim-3

Slim Controller Request, response and args not available


What I need is : Use a custom class to receive a HTTP Request and deal with it.

What I have so far:

$app->group('/user', function () use ($app) {
   $app->post('/login', Auth::class . ':login');
}

And in my Auth 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
}

My code shows in the function login what I need : get the http params, and the request/response/args params. ( I want to implement many functions in one class, I mean, I don't want to use __invoke()) Another thing I don't understand is, if I do something like return $response->withJson($somearraydata,200); the variable $response actually works. Why?

Thanks for any help!


Solution

  • I think I have figured it out,
    $args are never set (unless it is a get),but request and response are.

    and, to get params, I could do: $request->getParsedBody()['attribute']

    Hope this helps someone. :)