I am using slim version 3.8 and I want to disable outputBuffering and show all errors:
Following is my simple Hello World program.
require_once(dir.‘/vendor/autoload.php’);
$configuration = [
‘settings’ => [
‘displayErrorDetails’ => true,
“outputBuffering” => false,
],
];
$app = new \Slim\App($configuration);
// Add route callbacks
$app->get(‘/’, function ($request, $response, $args) {
echo “here”;
return $response->withStatus(200)->write(‘Hello World!’);
});
// Run application
$app->run();
If I comment echo “here”; then it works fine.
But this code is throwing exception - 500 internal error saying that “Unexpected data in output buffer. Maybe you have characters before an opening”.
Can someone please tell me what’s going wrong here?
I copied and pasted that code into my index.php
file, added <?php
to a blank line at the top and changed the require line to require __DIR__ . '/../vendor/autoload.php';
That is working fine for me. Also make sure you are not using a closing php tag at the bottom of the file.
Here is my index.php
, it works with the echo line commented out as well.
<?php
require __DIR__ . '/../vendor/autoload.php';
$configuration = [
'settings' => [
'displayErrorDetails' => true,
"outputBuffering" => false,
],
];
$app = new \Slim\App($configuration);
// Add route callbacks
$app->get('/', function ($request, $response, $args) {
echo "here";
return $response->withStatus(200)->write('Hello World!');
});
// Run application
$app->run();
I tried your code again. It’s still not working in my case. Following is my index.php file as corrected by you and there is no blank space at top and I am not using closing php tag.
<?php
require __DIR__ . '/../vendor/autoload.php';
$configuration = [
'settings' => [
'displayErrorDetails' => true,
"outputBuffering" => false,
],
];
$app = new \Slim\App($configuration);
// Add route callbacks
$app->get('/', function ($request, $response, $args) {
echo "here";
return $response->withStatus(200)->write('Hello World!');
});
// Run application
try {
$app->run();
} catch (\Exception $e) {
print_r($e);// it's catching exception here.
}
In my case, it is throwing exception and I am getting 500 internal server error. If I comment out echo “here”, then it works fine.
Is it really running in your case without commenting out echo “here”??
I’ve fixed my problem doing this:
<?php
$configuration = [
'settings' => [
'displayErrorDetails' => true,
"outputBuffering" => false,
'addContentLengthHeader' => false,
],
];
I added the attribute addContentLengthHeader with the value false in the settings array and this solved problem.
By default, Slim adds a Content-Length header to the response with the length of the response. If there are characters added outside of the Slim response, the value for the Content-Length would be incorrect and this would result in characters being cut off in the browser (the browser ignores data after the content-length bytes).
Yes, without commenting out the echo and without adding the ‘addContentLengthHeader’. The reason/difference must be elsewhere. But, if I put in your try/catch then I get a 500 error.