简体   繁体   中英

Slim Framework returning response without 'return $response'

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.

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM