Unreasonable Errors: Slim 3 Middleware and Respect/Validation

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

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

The error sais you sent some response before slim got to it, which can be caused by some whitespace before your php opening tag

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.

Is the code hosted online?

Also no whitespace before the php tag in the middleware itself? (Since the ValidationErrorsMiddleware code block you posted has whitespace)

Nope. They are hosted locally. I use Wamp 64 bit.

I removed the var_dump(‘middleware’) in ValidationErrorsMiddleware, and add this code

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

and I got this another weird things… not sure whether it is an error or not

<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: errors in C:\wamp64\www\authentication\app\Middleware\ValidationErrorsMiddleware.php on line <i>9</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0000</td><td bgcolor='#eeeeec' align='right'>236464</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\authentication\public\index.php' bgcolor='#eeeeec'>...\index.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0340</td><td bgcolor='#eeeeec' align='right'>2563544</td><td bgcolor='#eeeeec'>Slim\App->run(  )</td><td title='C:\wamp64\www\authentication\public\index.php' bgcolor='#eeeeec'>...\index.php<b>:</b>5</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>3</td><td bgcolor='#eeeeec' align='center'>0.0450</td><td bgcolor='#eeeeec' align='right'>3356808</td><td bgcolor='#eeeeec'>Slim\App->process(  )</td><td title='C:\wamp64\www\authentication\vendor\slim\slim\Slim\App.php' bgcolor='#eeeeec'>...\App.php<b>:</b>298</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>4</td><td bgcolor='#eeeeec' align='center'>0.0460</td><td bgcolor='#eeeeec' align='right'>3356976</td><td bgcolor='#eeeeec'>Slim\App->callMiddlewareStack(  )</td><td title='C:\wamp64\www\authentication\vendor\slim\slim\Slim\App.php' bgcolor='#eeeeec'>...\App.php<b>:</b>337</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>5</td><td bgcolor='#eeeeec' align='center'>0.0460</td><td bgcolor='#eeeeec' align='right'>3357072</td><td bgcolor='#eeeeec'>Slim\App->Slim\{closure}(  )</td><td title='C:\wamp64\www\authentication\vendor\slim\slim\Slim\MiddlewareAwareTrait.php' bgcolor='#eeeeec'>...\MiddlewareAwareTrait.php<b>:</b>116</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>6</td><td bgcolor='#eeeeec' align='center'>0.0460</td><td bgcolor='#eeeeec' align='right'>3357120</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.call-user-func:{C:\wamp64\www\authentication\vendor\slim\slim\Slim\MiddlewareAwareTrait.php:67}' target='_new'>call_user_func:{C:\wamp64\www\authentication\vendor\slim\slim\Slim\MiddlewareAwareTrait.php:67}</a>
(  )</td><td title='C:\wamp64\www\authentication\vendor\slim\slim\Slim\MiddlewareAwareTrait.php' bgcolor='#eeeeec'>...\MiddlewareAwareTrait.php<b>:</b>67</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>7</td><td bgcolor='#eeeeec' align='center'>0.0460</td><td bgcolor='#eeeeec' align='right'>3357264</td><td bgcolor='#eeeeec'>Slim\DeferredCallable->__invoke(  )</td><td title='C:\wamp64\www\authentication\vendor\slim\slim\Slim\MiddlewareAwareTrait.php' bgcolor='#eeeeec'>...\MiddlewareAwareTrait.php<b>:</b>67</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>8</td><td bgcolor='#eeeeec' align='center'>0.0460</td><td bgcolor='#eeeeec' align='right'>3377008</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.call-user-func-array:{C:\wamp64\www\authentication\vendor\slim\slim\Slim\DeferredCallable.php:37}' target='_new'>call_user_func_array:{C:\wamp64\www\authentication\vendor\slim\slim\Slim\DeferredCallable.php:37}</a>
(  )</td><td title='C:\wamp64\www\authentication\vendor\slim\slim\Slim\DeferredCallable.php' bgcolor='#eeeeec'>...\DeferredCallable.php<b>:</b>37</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>9</td><td bgcolor='#eeeeec' align='center'>0.0460</td><td bgcolor='#eeeeec' align='right'>3377600</td><td bgcolor='#eeeeec'>App\Middleware\ValidationErrorsMiddleware->__invoke(  )</td><td title='C:\wamp64\www\authentication\vendor\slim\slim\Slim\DeferredCallable.php' bgcolor='#eeeeec'>...\DeferredCallable.php<b>:</b>37</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-uncaught-exception' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Fatal error: Uncaught exception 'RuntimeException' with message 'Unexpected data in output buffer. Maybe you have characters before an opening &lt;?php tag?' in C:\wamp64\www\authentication\vendor\slim\slim\Slim\App.php on line <i>552</i></th></tr>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> RuntimeException: Unexpected data in output buffer. Maybe you have characters before an opening &lt;?php tag? in C:\wamp64\www\authentication\vendor\slim\slim\Slim\App.php on line <i>552</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0000</td><td bgcolor='#eeeeec' align='right'>236464</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\authentication\public\index.php' bgcolor='#eeeeec'>...\index.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0340</td><td bgcolor='#eeeeec' align='right'>2563544</td><td bgcolor='#eeeeec'>Slim\App->run(  )</td><td title='C:\wamp64\www\authentication\public\index.php' bgcolor='#eeeeec'>...\index.php<b>:</b>5</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>3</td><td bgcolor='#eeeeec' align='center'>0.0450</td><td bgcolor='#eeeeec' align='right'>3356808</td><td bgcolor='#eeeeec'>Slim\App->process(  )</td><td title='C:\wamp64\www\authentication\vendor\slim\slim\Slim\App.php' bgcolor='#eeeeec'>...\App.php<b>:</b>298</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>4</td><td bgcolor='#eeeeec' align='center'>0.1220</td><td bgcolor='#eeeeec' align='right'>6393544</td><td bgcolor='#eeeeec'>Slim\App->finalize(  )</td><td title='C:\wamp64\www\authentication\vendor\slim\slim\Slim\App.php' bgcolor='#eeeeec'>...\App.php<b>:</b>344</td></tr>
</table></font>

That code is produced by xdebug. In html it is this:

Thanks for converting those into readable form.

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

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

1 Like

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.

3 Likes

Thanks for the advice. I have re-enabled the addContentLengthHeader in the setting, and those errors are not re-appearing.

make sure you have no utf8 file with BOM

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

1 Like

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