Slim Framework returning response without 'return $response'

隐身守侯 提交于 2019-12-25 08:06:13

问题


I'm confused with how Slim is returning a response without return $response as per the documentation.

If I have the following code:

$app->get('/login', function ($request, $response, $args) {

    $response = $this->view->render($response, "login.php");
    return $response;  
});

When I call /login through my browser it renders my login.php template, which is what I'd expect.

But if I remove return $response it still works which doesn't seem right?

$app->get('/login', function ($request, $response, $args) {

    $response = $this->view->render($response, "login.php"); 
});

Both sets of code have the same output.

How is Slim showing the response if $response is not being returned?


回答1:


The body is a Psr\Http\Message\StreamInterface which is not immutable, when you append something to the body you normally do:

$body = $response->getBody();
$body->write($string);
$body->write($string2);

This changes the content of the stream but is still in the same $response-Object.

As the view renderer only appends to the body, there is no need to actually return the $response.



来源:https://stackoverflow.com/questions/40191474/slim-framework-returning-response-without-return-response

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!