我正在尝试使用 ValidationErrorsMiddleware.php 类作为中间件,因此我将以下代码添加到我的 bootstrap/app.php 中:
$app->add(new App\Middleware\ValidationErrorsMiddleware($container));
将上述代码添加到我的 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
以防万一,任何人都需要查看我的类和 app.php 的代码,我已将它们包含在此处
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;
}
}
中间件.php
<?php
namespace App\Middleware;
class Middleware {
protected $container;
public function __construct($container) {
$this->container = $container;
}
}
应用.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';
最佳答案
我已经解决了我的问题:
return [
'settings' => [
// Slim Settings
'determineRouteBeforeAppMiddleware' => true,
'displayErrorDetails' => true,
'addContentLengthHeader' => false,
我在设置数组中添加了值为 false 的属性 addContentLengthHeader。
但是我还是不明白这是干什么的
更新
发生此问题是因为 var_dump(middleware) 行更改了响应的内容长度。我的解决方案只是一个黑客。 感谢@iKlsR 提供正确答案。
关于php - PHP Slim 3 中间件的不合理错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37293280/