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

8 回答 8

13

我已经解决了这样做的问题:

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

我在设置数组中添加了值为 false 的属性 addContentLengthHeader。

但我还是不明白这是干什么用的

更新

发生此问题是因为 var_dump(middleware) 行改变了响应的内容长度。我的解决方案只是一个 hack。感谢@iKlsR 的正确答案。

于 2016-05-18T15:22:38.283 回答
6

设置addContentLengthHeader为 false 不是正确的解决方法,并且可能会在您的应用程序变大后导致问题。您的问题是var_dump('middleware');您在返回响应之前正在打印的问题。这会使Content-Length标题的大小不正确,从而导致错误,因为在此之外还有字符。如果您有错误报告,php 还应该通过让您了解有关意外数据的信息来暗示这一点。

要使用语句测试或修改您的中间件,请使用$response->getBody()->write('message');简单的代码编辑响应主体,die('message');以查看它是否被命中。

于 2016-06-22T16:06:43.283 回答
1

我遇到了同样的问题,因为我$app->run();两次调用了该语句。我刚刚删除了其中一个,一切正常(保持:addContentLengthHeaderas true)。

于 2017-05-31T14:26:56.997 回答
0

我对给定的教程有同样的问题,致命错误是由这一行产生的:

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

启动$_SESSION['errors']时未设置,在这种情况下,我们会收到导致致命错误的通知

我做了什么,我在启动时检查是否$_SESSION['errors']已设置

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

    unset($_SESSION['errors']);
于 2017-04-12T07:33:15.330 回答
0

就我而言,我有一个双重$app->run();密码。

于 2019-06-17T09:57:16.070 回答
0

如果对于某人,上述所有解决方案都不起作用,您需要检查您包含的任何文件是否未编码为UTF8-Bom(或任何格式-Bom),此处的此答案有助于解决问题,并且如果您选择正确的格式,也可以通过 vscode 完成

于 2019-09-13T19:54:46.850 回答
0

它实际上是一个与缓冲有关的缓冲区问题,要么通过进入“ php.ini ”文件关闭缓冲区,要么在脚本开头使用ob_end_clean() ......

于 2017-09-07T05:56:32.200 回答
0

只有您在索引中编辑了下一个:

$app = new \Slim\App([
    'settings' => [
        'addContentLengthHeader' => false
    ]
]);
于 2019-09-26T18:46:21.850 回答