2017-11-20 6 views
0

私はSlim 3というPHPフレームワークを使用しています。私はResponseオブジェクトを検査したいと思います。スリム3レスポンスオブジェクトのデータにアクセス

公式文書では、Response`オブジェクトがPSR 7 ResponseInterfaceを実装していることを示しています。

私がvar_dump($response->getBody();の場合は、protectedのボディのみが表示され、$stackのコンテンツはどこにも見つかりません。

ドキュメントにはこれが記載されており、これが理由だと思います。これを確認できますか?

備考 レスポンスオブジェクトは不変です。このメソッドは、新しい本文を含むResponseオブジェクトのコピーを返します。

出典:のvar_dumpのhttps://www.slimframework.com/docs/objects/response.html

PHPコントローラクラス

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

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

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

出力

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

答えて

1

コンテンツは、HTTPレスポンスボディストリームに書き込まれています。

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

これはあなたが一般的に持っている特殊なPHP知識か、この回答を私に提供するために検索しなければならないものかどうか尋ねてもらえますか?私はこれが驚くべきことだと思う。 – Magiranu

+1

[PSR-11](http://www.php-fig.org/psr/psr-7/)のドキュメントには、この情報が含まれています。私はそれがうまくいくまで何かを探して試しなければならなかった。 – DanielO

関連する問題