Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在路由执行前后修改slim v3响应体?_Php_Slim - Fatal编程技术网

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

Php 如何在路由执行前后修改slim v3响应体?,php,slim,Php,Slim,我无法在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['determineRouteBeforeAppMiddlewar

我无法在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();

尝试在中间件
mw
上更改
getContents()
。另一个应该做的更改是在
mw2
:您必须返回创建的新响应

查看完整的代码:

<?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();

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

方法#1

方法#2

我无法在slim v3中获得响应主体,它总是空白的

我也遇到了同样的问题,无法理解为什么会得到空体响应,因此我最终向返回变量添加了
json\u encode($my\u value)
,这就是神奇之处。现在我可以开始买一只新狗了,也许还可以结婚了。希望这对别人有帮助

/**
 * 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;
    }
}

$body=$response->getBody()->getContents()。。。在获取内容之前,需要倒带正文。此$body=$response->getBody()->getContents();正在给空字符串。你能提供一些使用倒带的例子吗?我尝试了倒带,但仍然没有成功…您需要$response->getBody()->倒带()。。。然后获取内容。我尝试了相同的\uuuuToString(),但不起作用。你的代码运行正常,最终我实现了我所追求的目标。谢谢你,朋友。。。
/**
 * 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;
    }
}