简体   繁体   English

PHP Slim 3 中间件的不合理错误

[英]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:我试图使用 ValidationErrorsMiddleware.php 类作为中间件,所以我将以下代码添加到我的 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:将上述代码添加到我的 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以防万一,任何人都需要查看我的类和 app.php 的代码,我已将它们包含在此处


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

Middleware.php中间件.php

<?php

namespace App\Middleware;

class Middleware {

protected $container;

  public function __construct($container) {

    $this->container = $container;
  }
}

App.php应用程序.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';

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.我在设置数组中添加了值为 false 的属性 addContentLengthHeader。

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.发生此问题是因为行 var_dump(middleware) 更改了响应的内容长度。 My solution was just a hack.我的解决方案只是一个黑客。 Thanks to @iKlsR for the right answer.感谢@iKlsR 提供正确答案。

Setting addContentLengthHeader to false is not a proper fix and can lead to woes later on when your app gets larger.addContentLengthHeader设置为 false 不是一个正确的解决方法,并且可能会在您的应用程序变大时导致稍后出现问题。 Your problem is the var_dump('middleware');你的问题是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.这会使您的Content-Length标头的大小不正确,从而导致错误,因为在此之外还有字符。 php should also hint at this by letting you know something about unexpected data if you have error reporting on.如果您有错误报告,php 还应该通过让您了解意外数据来暗示这一点。

To test or modify your middleware with statements, edit the response body with $response->getBody()->write('message');要使用语句测试或修改您的中间件,请使用$response->getBody()->write('message');编辑响应正文$response->getBody()->write('message'); tho a simple die('message'); tho 一个简单的die('message'); should be good enough to see if it was hit.应该足够好,看看它是否被击中。

I had the same problem because I called the $app->run();我遇到了同样的问题,因为我调用了$app->run(); statement twice.声明两次。 I just deleted one of them and everything worked well (keeping: addContentLengthHeader as true ).我刚刚删除了其中一个,一切运行良好(保持: addContentLengthHeadertrue )。

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启动时的$_SESSION['errors']未设置,在这种情况下,我们收到一条通知,导致致命错误

what I made, I check on boot if $_SESSION['errors'] is set我所做的,如果设置了$_SESSION['errors'] ,我会检查启动

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

    unset($_SESSION['errors']);

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

In my case I had a double $app->run();就我而言,我有一个双$app->run(); code.代码。

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

only you've edit the next in your index:只有您编辑了索引中的下一个:

$app = new \Slim\App([
    'settings' => [
        'addContentLengthHeader' => false
    ]
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM