# $Request is empty/null inside my own controller

**URL:** <https://discourse.slimframework.com/t/request-is-empty-null-inside-my-own-controller/841>\
**Category:** Questions\
**Created:** [October 24, 2016, 5:07pm UTC](https://discourse.slimframework.com/t/request-is-empty-null-inside-my-own-controller/841 "2016-10-24T17:07:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![3HMonkey](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/3hmonkey/32/246_2.png) [@3HMonkey](https://discourse.slimframework.com/u/3HMonkey)\
**Post date:** [October 24, 2016, 5:07pm UTC](https://discourse.slimframework.com/t/request-is-empty-null-inside-my-own-controller/841/1 "2016-10-24T17:07:34Z")

</div>

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):

```php
$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.

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

```

```php
<?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
<?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:

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

```

Really thanks a lot for your time!!!

---

<div class="post-metadata">

**Author:** ![geggleto](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/geggleto/32/11_2.png) [@geggleto](https://discourse.slimframework.com/u/geggleto)\
**Post date:** [October 24, 2016, 5:13pm UTC](https://discourse.slimframework.com/t/request-is-empty-null-inside-my-own-controller/841/2 "2016-10-24T17:13:53Z")

</div>

remove all your middleware and retest.

i suspect there’s a problem in your middleware.

---

<div class="post-metadata">

**Author:** ![3HMonkey](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/3hmonkey/32/246_2.png) [@3HMonkey](https://discourse.slimframework.com/u/3HMonkey)\
**Post date:** [October 24, 2016, 5:21pm UTC](https://discourse.slimframework.com/t/request-is-empty-null-inside-my-own-controller/841/3 "2016-10-24T17:21:30Z")

</div>

Wow that was a quick reply 🙂 … 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.

---

<div class="post-metadata">

**Author:** ![geggleto](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/geggleto/32/11_2.png) [@geggleto](https://discourse.slimframework.com/u/geggleto)\
**Post date:** [October 24, 2016, 5:30pm UTC](https://discourse.slimframework.com/t/request-is-empty-null-inside-my-own-controller/841/4 "2016-10-24T17:30:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![3HMonkey](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/3hmonkey/32/246_2.png) [@3HMonkey](https://discourse.slimframework.com/u/3HMonkey)\
**Post date:** [October 24, 2016, 5:42pm UTC](https://discourse.slimframework.com/t/request-is-empty-null-inside-my-own-controller/841/5 "2016-10-24T17:42:34Z")

</div>

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

```php
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:

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

```

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

```php
<?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();

```

---

<div class="post-metadata">

**Author:** ![3HMonkey](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/3hmonkey/32/246_2.png) [@3HMonkey](https://discourse.slimframework.com/u/3HMonkey)\
**Post date:** [October 24, 2016, 6:00pm UTC](https://discourse.slimframework.com/t/request-is-empty-null-inside-my-own-controller/841/6 "2016-10-24T18:00:13Z")

</div>

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!!!
