php - 访问 Slim 3 响应对象的数据

标签 php slim slim-3

我正在使用名为 Slim 3 的 PHP 框架,我想检查我的 Response 对象,但我想知道为什么这是不可能的。

官方文档指出 Response` 对象实现了 PSR 7 ResponseInterface,它允许我检查主体。

当我 var_dump($response->getBody(); 时,我只看到 protected 主体,而找不到我的 $stack 内容随处可见。

文档说明了这一点,我认为这就是原因。您能确认一下吗?

Reminder The Response object is immutable. This method returns a copy of the Response object that contains the new body.

来源:https://www.slimframework.com/docs/objects/response.html

PHP Controller 类

class Controller
{
    public function index($request, $response, $args)
    {
        // code

        $stack    = array($cars, $manufacturers);
        $response = $response->withJson($stack);

        return $response->withStatus(200);
    }
}

var_dump 的输出

class Slim\Http\Response#201 (5) {
  protected $status =>
  int(200)
  protected $reasonPhrase =>
  string(2) "OK"
  protected $protocolVersion =>
  string(3) "1.1"
  protected $headers =>
  class Slim\Http\Headers#200 (1) {
    protected $data =>
    array(1) {
      'content-type' =>
      array(2) {
        ...
      }
    }
  }
  protected $body =>
  class Slim\Http\Body#202 (7) {
    protected $stream =>
    resource(87) of type (stream)
    protected $meta =>
    array(6) {
      'wrapper_type' =>
      string(3) "PHP"
      'stream_type' =>
      string(4) "TEMP"
      'mode' =>
      string(3) "w+b"
      'unread_bytes' =>
      int(0)
      'seekable' =>
      bool(true)
      'uri' =>
      string(10) "php://temp"
    }
    protected $readable =>
    NULL
    protected $writable =>
    bool(true)
    protected $seekable =>
    NULL
    protected $size =>
    NULL
    protected $isPipe =>
    NULL
  }
}

最佳答案

内容写入 HTTP 响应正文流。

$content = $response->getBody()->__toString();

$content = (string)$response->getBody();

关于php - 访问 Slim 3 响应对象的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47394393/

相关文章:

php - 仅应通过引用传递变量 - 当将 LIMIT 与 bindParam 一起使用时

php - 实现 LIMIT 和 OFFSET 时出现语法错误

php - Slim 3 框架 - 将数据从中间件传递到 Controller - Action 参数

PHP:提供文件结构中没有 .php 文件的页面

PHP 邮件脚本运行多次(使用 Mailgun)

php - 将 MySQL 更改推送/发送给客户端

php - Slim Framework v3,通过 POST 问题获取 JSON 数据

slim - 具有相同签名的多个 Slim 路由

PHP XPath 问题

PHP查询mysql数据库,echo长名称换取短表值