The problem with redirect of the Slim4

It’s been a while since I started my study of slim4, and I’m having a lot of difficulty understanding why it doesn’t redirect the route when a message is displayed on the screen.
If anyone could explain it to me I would greatly appreciate it.
ex:

<?php

namespace App\controllers;

class Home {

    public function home($request, $response) {

        echo "Hello World.";

        return $response->withHeader('Location', '/admin')->withStatus(301);

    }

}

The issue is that a redirect response must have an empty body. The echo statement sends content to the output buffer, which prevents Slim from sending headers. To resolve this, remove the echo statement.

1 Like

Or you can (I think) set a middleware to ob_clean() output buffer in case of specific headers requested

1 Like