Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unreasonable Errors on PHP Slim 3 Middleware

I am trying to use the ValidationErrorsMiddleware.php class as a 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 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 needs to look at the code of my classes and app.php, I have included them down 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';
like image 739
DarkSpirit Avatar asked May 18 '16 07:05

DarkSpirit


3 Answers

I've fixed my problem doing this:

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

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

But I still did not understand what this is for

UPDATE

This problem happens because of the line var_dump(middleware) thats changes the content length of the response. My solution was just a hack. Thanks to @iKlsR for the right answer.

like image 137
Marcio Barroso Avatar answered Nov 16 '22 22:11

Marcio Barroso


Setting addContentLengthHeader to false is not a proper fix and can lead to woes later on when your app gets larger. Your problem is the var_dump('middleware'); which you are printing before you return the response. This makes the size of your Content-Length header incorrect, thus the error, since there are characters outside of this. php should also hint at this by letting you know something about unexpected data if you have error reporting on.

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

like image 6
iKlsR Avatar answered Nov 16 '22 21:11

iKlsR


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

like image 1
lerox Avatar answered Nov 16 '22 20:11

lerox