6

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';
alexw
  • 8,468
  • 6
  • 54
  • 86
DarkSpirit
  • 307
  • 1
  • 3
  • 6
  • The same problem starts to happen with me since I've run composer again. I am using "slim/slim": "^3.0" ... I've compared the code of App.php and found a new code that throws this exception, but I did not understand the cause of that. – Marcio Barroso May 18 '16 at 15:14

8 Answers8

14

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.

Marcio Barroso
  • 783
  • 1
  • 7
  • 21
  • 2
    Awesome!! It really works :)) I had been struggling with the problem for a day, and now I can continue working on the authentication things. Thanks. – DarkSpirit May 18 '16 at 16:38
  • I think i might have a good guess about the `addContentLengthHeader`: I had the same issue, and when i turned off, the output showed with the extra PHP `E_NOTICE` which was html formatted. So my guess is that Slim counts the expected length, and before output it compares it to the actual length, and the error got in there some other way - so the two length mismatched and that caused an exception. – zedling May 27 '16 at 11:20
  • You should change the accepted answer to iKlsR's answer, since yours is a 'hack' and not a true solution ;) No offense – zedling Apr 12 '17 at 07:37
  • 1
    @MarcioBarroso this is not up to you, it's the asker who does this, no worries all the answers are visible, I wouldn't say your approach was a hack but it can have some adverse effects down the line. It's good to know what the [configuration](https://www.slimframework.com/docs/objects/application.html#application-configuration) options do instead of just blindly turning them off to skip an error. – iKlsR Jul 26 '17 at 21:36
6

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.

iKlsR
  • 2,642
  • 6
  • 27
  • 45
  • 2
    the code in this question is follow https://www.youtube.com/watch?v=0hKciR_dJAk&index=12&list=PLfdtiltiRHWGc_yY90XRdq6mRww042aEC. Can you explain why in this video, they don't get the error with header but me and the OP got – Linh Aug 27 '16 at 17:10
1

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).

lerox
  • 5
  • 2
0

I had the same issue with the given tutorial, the Fatal error is produced by this line:

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

the $_SESSION['errors'] on boot is not set and in this case we receive a Notice which causes a Fatal Error

what I made, I check on boot if $_SESSION['errors'] is set

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

    unset($_SESSION['errors']);
fefe
  • 8,755
  • 27
  • 104
  • 180
0

Its actually a buffer problem related to buffering either turn off the buffer by going into the "php.ini" file or simply use ob_end_clean() at the beginning of your script...

0

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

Tobia
  • 9,165
  • 28
  • 114
  • 219
0

If for someone all the solutions above didn't work, you will need to check out if any of the files you're including is not coded as UTF8-Bom (or whatever-format-Bom), this answer here help to fix the problem, and can be done through vscode too if you select the correct format

William-H-M
  • 1,050
  • 1
  • 13
  • 19
0

only you've edit the next in your index:

$app = new \Slim\App([
    'settings' => [
        'addContentLengthHeader' => false
    ]
]);
david pincheira
  • 171
  • 1
  • 6