Unreasonable errors on PHP Slim 3 Middleware

I am trying to use the ValidationErrorsMiddleware.php class as middleware, so I added the following code to my bootstrap / app.php:

$app->add(new App\Middleware\ValidationErrorsMiddleware($container)); 

I got the following errors after the above code was 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, someone needs to look at the code of my classes and app.php, I included them here


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; } } 

app.php

 <?php session_start(); require __DIR__ . '/../vendor/autoload.php'; $app = new \Slim\App([ 'settings' => [ 'determineRouteBeforeAppMiddleware' => false, 'displayErrorDetails' => true, 'db' => [ // Eloquent configuration 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'phpdb', 'username' => 'root', 'password' => 'root', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ] ], ]); $container = $app->getContainer(); $app->add(new App\Middleware\ValidationErrorsMiddleware($container)); require __DIR__ . '/../app/routes.php'; 
+5
source share
8 answers

I fixed my problem:

 return [ 'settings' => [ // Slim Settings 'determineRouteBeforeAppMiddleware' => true, 'displayErrorDetails' => true, 'addContentLengthHeader' => false, 

I added the addContentLengthHeader attribute with false in the settings array.

But I still do not understand what it is for

UPDATE

This problem occurs because of the var_dump (middleware) line, which changes the length of the response content. My decision was just a hack. Thanks @iKlsR for the correct answer.

+12
source

Setting addContentLengthHeader to false is not a correct fix and may lead to disasters later when your application grows larger. Your problem is var_dump('middleware'); which you print before returning the answer. This makes the size of your Content-Length header incorrect, therefore an error, since there are characters outside this character. php should also hint at this by informing you of unexpected data if you have a bug report.

To check or modify your middleware using instructions, edit the response body with $response->getBody()->write('message'); , but a simple die('message'); should be good enough to see if it was hit.

+6
source

I had the same problem because I called the $app->run(); statement twice $app->run(); . I just deleted one of them and it worked fine (keeping: addContentLengthHeader as true ).

+1
source

I had the same problem with this guide, Fatal error is generated on this line:

 $this->container->view->getEnvironment()->addGlobal('errors', $_SESSION['errors']); unset($_SESSION['errors']); 

$_SESSION['errors'] not set at boot, in which case we get a notification that causes a fatal error

what i did i check boot if $_SESSION['errors'] set

 if(isset($_SESSION['errors'])) { $this->container->view->getEnvironment()->addGlobal('errors', $_SESSION['errors']); } unset($_SESSION['errors']); 
0
source

In fact, a buffer problem related to buffering disables the buffer by going to the php.ini file or just use ob_end_clean () at the beginning of your script ...

0
source

In my case, I had a double $app->run(); code.

0
source

If for some people all of the above solutions did not work, you need to check if any of the files that you include does not contain the UTF8-Bom encoding (or any other Bom format), this answer here will help solve the problem, and this can be done via vscode if you select the correct format

0
source

only you edit the following in your index:

 $app = new \Slim\App([ 'settings' => [ 'addContentLengthHeader' => false ] ]); 
0
source

All Articles