如何在路由执行前后修改 slim v3 响应体?

How to modify slim v3 response body before and after route execution?

我无法在 slim v3 中获取响应正文,它始终为空白。我的代码是:

<?php

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use \Slim\App as Slim;

require 'vendor/autoload.php';

$config['determineRouteBeforeAppMiddleware'] = true;

$app = new Slim(['settings' => $config]);

$mw = (function (Request $request, Response $response, callable $next) {
    $response = $response->withStatus(200)->write(' before ');
    $response = $next($request, $response);
    $body = $response->getBody()->getContents();
    $response = $response->withJson(array('data' => $body)); // output should be {"data":" Hello, User  seq1  seq2 "}
    return $response;
});

$mw1 = (function (Request $request, Response $response, callable $next) {
    $response = $next($request, $response);
    $response = $response->withStatus(200)->write(' seq1 ');
    return $response;
});

$mw2 = (function (Request $request, Response $response, callable $next) {
    $response = $next($request, $response);
    $response->withStatus(200)->write(' seq2 ');
    return $response;

});

$app->add($mw);

$app->get('/hello/{name}', function (Request $request, Response $response) {
    $name = $request->getAttribute('name');
    $response->getBody()->write(" Hello, $name ");
    return $response;
})->add($mw1)->add($mw2);

$app->run();

我想做的是:

P.S。 Slim v2 比 Slim v3 容易得多

尝试在中间件 mw 处为 __toString() 更改 getContents()。另一个应该做的改变是 mw2:你必须 return 创建新的响应。

查看完整代码:

<?php

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use \Slim\App as Slim;

require 'vendor/autoload.php';

$config['determineRouteBeforeAppMiddleware'] = true;

$app = new Slim(['settings' => $config]);

$mw = (function (Request $request, Response $response, callable $next) {
    $response = $response->withStatus(200)->write(' before ');
    $response = $next($request, $response);
    $body = $response->getBody()->__toString();
    $response = $response->withJson(array('data' => $body)); // output should be {"data":" Hello, User  seq1  seq2 "}
    return $response;
});

$mw1 = (function (Request $request, Response $response, callable $next) {
    $response = $next($request, $response);
    $response = $response->withStatus(200)->write(' seq1 ');
    return $response;
});

$mw2 = (function (Request $request, Response $response, callable $next) {
    $response = $next($request, $response);
    $response = $response->withStatus(200)->write(' seq2 ');
    return $response;

});

$app->add($mw);

$app->get('/hello/{name}', function (Request $request, Response $response) {
    $name = $request->getAttribute('name');
    $response->getBody()->write(" Hello, $name ");
    return $response;
})->add($mw1)->add($mw2);

$app->run();

希望对您有所帮助

PS: 我更喜欢 Slim 3 :D

您也可以通过这种方式获取内容

方法一

$streamBody = $response->getBody();
$streamBody->rewind();
$content = $streamBody->getContents();

方法二

$streamBody = $response->getBody();
$streamBody->rewind();
$content = $streamBody->read($streamBody->getSize());

I'm unable to get response body in slim v3 and its always blank.

我有同样的问题,无法弄清楚为什么我得到 空体响应 ,所以我最终将 json_encode($my_value) 添加到我的 return 变量,这就是魔法。现在我可以继续买一只新狗,并可能结婚。希望这对某人有所帮助。

/**
 * method - GET
 */
$app->get('/posts[/]', function ($request, $response, $args) {
    $db = new DbHandler();
    // fetching all
    $result = $db->getAllPosts();
    return json_encode($result);
});

另一边

/**
     * GetPosts
     */
    public function getAllPosts() {
    // Instantiate DBH
    // make a connection to mysql here
    $db = new PDO_Wrapper();
    $db->query("SELECT * FROM all_posts WHERE post_status = :Publish AND post_type = :Post ORDER BY post_date DESC");
    $binding_array = array(":Publish" => "publish", ":Post" => "post");
    $db->bindArray($binding_array);
    $results = $db->resultset();
    return $results;
    }
}