$Request is empty/null inside my own controller

Hello guys,

I already read through the net and tried to find a solution for my problem. I also posted this to the issue section on git before I found this. Sorry about that. You can delete the git-post.
First I want to say that I have been building a REST API using the slim framework.
I just want to give you an example (which is working fine):

$app->post('/login', function ($request, $response, $args) {


    $response_array = array(
        "status" => "sucess",
        "data" => array(),
        "message" => null



    );

    $parsedBody = $request->getParsedBody();


    $validate = new Validate();
    $validation = $validate->check($parsedBody, array(
            'username' => array(
                'required' => true,


            ),
            'password' => array(
                'required' => true,
                'min' => 10

            )

        )
    );

    if($validation->passed()){

        echo 'working';

    }else{


        $response->write(json_encode($response_array));

    }



    return $response;


});

Well this is working fine while sending a JSON object to “/login”. But my index.php is getting bigger and bigger now, so I decided start using controllers by creating an abstract class which represents the main controller interface.

$app->get('/', \Routes\RootController::class);
$app->post('/login', \Routes\LoginController::class);
<?php

namespace Routes;



use Interop\Container\ContainerInterface;



abstract class MainController
{
    protected $ci;

    public function __construct(ContainerInterface $ci)
    {
        $this->ci = $ci;
    }

    abstract public function __invoke($request, $response, $args);
}

Then I created the LoginController.

<?php


namespace Routes;


class LoginController extends MainController
{

    public function __invoke($request, $response, $args)
    {
        $response_array = array(
            "status" => "sucess",
            "data" => array(),
            "message" => null



        );



        $parsedBody = $request->getParsedBody();


        $validate = new \Validate();
        $validation = $validate->check($parsedBody, array(
                'username' => array(
                    'required' => true,


                ),
                'password' => array(
                    'required' => true,
                    'min' => 10

                )

            )
        );

        if($validation->passed()){

            echo 'working';


        }else{


            $response->write(json_encode($response_array));

        }
        return $response;
    }
}

And here the problem came up. It seems that the “$request” is empty or null. Also by checking it with:

if(!$request){
echo 'not null';
}

Really thanks a lot for your time!!!

remove all your middleware and retest.

i suspect there’s a problem in your middleware.

Wow that was a quick reply :slight_smile: … Btw I am just using this Controller and Monolog which is not in use. If I remove this controller it is working fine. That means I don’t use any middleware except the slim standards.

Request object then would never be null unless you have added a middleware that is passing null up the middleware stack…

I noticed it is not really null. Here is the content if I use:

print_r($request->getBody())

The body:

Slim\Http\RequestBody Object ( [stream:protected] => Resource id #47 [meta:protected] => [readable:protected] => [writable:protected] => [seekable:protected] => [size:protected] => [isPipe:protected] => )

getPasedBody() returns null, which is running nice without the controller

I am sending:

{
"username":"test",
"password":"1234567890"
}

Here is my index.php. I think its really clean

<?php


require './vendor/autoload.php';
require_once './Framework/init.php';

$app = new Slim\App([
    'settings' => [
        'displayErrorDetails' => true,

        // Monolog settings
        'logger' => [
            'name' => 'slim-app',
            'path' => __DIR__ . '/logs/app.log',
        ],
    ],
]);

// Set up dependencies
$container = $app->getContainer();

// monolog
$container['logger'] = function ($c) {
    $settings = $c->get('settings')['logger'];
    $logger = new Monolog\Logger($settings['name']);
    $logger->pushProcessor(new Monolog\Processor\UidProcessor());
    $logger->pushHandler(new Monolog\Handler\StreamHandler($settings['path'], Monolog\Logger::WARNING));
    return $logger;
};




$app->get('/', \Routes\RootController::class);
$app->post('/login', \Routes\LoginController::class);




$app->get('/hello[/{name}]', function ($request, $response, $args) {

    $user = new User();

    if ($user->isLoggedIn()) {
        $response->write("Hello, " . $args['name']);


    } else {
        $response->write("Du musst dich einloggen");

    }
    return $response;

})->setArgument('name', 'World!');


$app->run();

Impossible how stupid a man could be… I FORGOT setting the “Content-type” to “application/json”… umpf … that was really a noob mistake… it is working now. Thank you for your time. So sorry again!!!