I got the following errors after the above code is added to my app.php:
Fatal error: Uncaught exception 'RuntimeException' with message 'Unexpected data in output buffer. Maybe you have characters before an opening <?php tag?' in C:\wamp64\www\authentication\vendor\slim\slim\Slim\App.php on line 552
RuntimeException: Unexpected data in output buffer. Maybe you have characters before an opening <?php tag? in C:\wamp64\www\authentication\vendor\slim\slim\Slim\App.php on line 552
Just in case anyone is curious about the code of the classes and app.php, I have included them down.
ValidationErrorsMiddleware.php
<?php
namespace App\Middleware;
class ValidationErrorsMiddleware extends Middleware {
public function __invoke($request, $response, $next) {
var_dump('middleware');
$response = $next($request, $response);
return $response;
}
}
Middleware.php
<?php
namespace App\Middleware;
class Middleware {
protected $container;
public function __construct($container) {
$this->container = $container;
}
}
Yes, you are right, but both errors are pointing at the \vendor\slim\slim\Slim\App.php on line 552 which is a file I got via composer. I do not modify any files in vendor\slim\slimā¦
I am sure that I have no whitespace before my php opening tag in my scripts, because those errors only occur when I introduced the $app->add(new App\Middleware\ValidationErrorsMiddleware($container)); to my app.php.
I am not sure how to solve thatā¦ and those two errors are still there.
Fatal error: Uncaught exception 'RuntimeException' with message 'Unexpected data in output buffer. Maybe you have characters before an opening <?php tag?' in C:\wamp64\www\authentication\vendor\slim\slim\Slim\App.php on line 552
RuntimeException: Unexpected data in output buffer. Maybe you have characters before an opening <?php tag? in C:\wamp64\www\authentication\vendor\slim\slim\Slim\App.php on line 552
The explanation is somewhat technical, but here goes:
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.
For example:
<?php
print '12'; // output before Slim starts
$app = new Slim\App();
$app->get('/', function (Slim\Http\Request $request,Slim\Http\Response $response, $args) {
$response->write('345');
});
$app->run(); // Slim output
print '67'; // output after Slim stopped
Would result in Slim added a Content-Length header with the value 3, but the actual page output size is 7 (2 characters before Slim runs, 3 characters as part of the Slim output and 2 characters after Slim runs. The output in the browser would be ā123ā, not ā1234567ā. To avoid this, Slim throws an Exception and notifies you of the fact that there was output before Slim started.
As @JoeBengalen mentioned, you probaby have enters or spaces before a <?php tag. These enters count as output (you will see these in the HTML source of the output). Slim notices this and throws an Exception, because this would probably lead to a wrong Content-Length header value.
By disabling the addContentLengthHeader setting, you disabled check for data in the output. As the empty lines are probably not intended you are better of removing them and re-enable the addContentLengthHeader setting.
Yes , I have found following error when I have used the same codes .
Notice: Trying to get property of non-object in C:\xampp\htdocs\myapp\app\Middleware\ValidationErrorsMiddleware.php on line 18
Fatal error: Call to a member function getEnvironment() on null in C:\xampp\htdocs\myapp\app\Middleware\ValidationErrorsMiddleware.php on line 18