Slim: V4: routes can no longer return null?

I had this route:

        $app->get('/', function () {
            // nothing
        });

It doesn’t seem to work anymore with v4.0.0:

TypeError : Return value of Slim\Handlers\Strategies\RequestResponse::__invoke() must implement interface Psr\Http\Message\ResponseInterface, null returned

Is this something that changed in v4?

I read both the documentation and the upgrade guide and couldn’t find any mention of this.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 26 (23 by maintainers)

Most upvoted comments

An argument for allowing not returning a response from the route might be all the examples out there (from official docs), and the developer’s need to quickly brush up a test route.

Consider these examples from official Slim docs. They do not return a response:

From Slim-Flash:

$app->get('/bar', function ($req, $res, $args) {
    // Get flash messages from previous request
    $messages = $this->flash->getMessages();
    print_r($messages);

    // Get the first message from a specific key
    $test = $this->flash->getFirstMessage('Test');
    print_r($test);
});

From Slim 4 itself:

$app->get('/hello/{name}', function (Request $request, Response $response, $args) {
    $name = $args['name'];
    echo "Hello, $name";
});

Ultimately, the main argument I see is make the developer’s life easier, which is the whole point behind a framework (at least it was for me when I chose Slim 2.) This error is a distraction.

Can let the framework fall back to a default value when no response is provided. Ensures backward compatibility with Slim 2 and 3.