The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Is there a way to stream the response with Slim?

John's Avatar

John

05 Mar, 2014 02:54 PM

I have a route that returns a very large set of data for graphing, and it exceeds PHP's memory settings. Rather than just arbitrarily increasing the memory limit for PHP, I'd like to just return database results line by line. Does slim support any ability to do this? From what I can see the entire response has to be set and then slim will return it.

  1. 1 Posted by Blake on 14 May, 2014 07:53 PM

    Blake's Avatar

    This has been brought up here:
    https://github.com/codeguy/Slim/issues/205
    At the bottom of the thread codeguy mentions that steaming is available in the development branch right now and will be part of the 3.0 release.

    The dev branch has sendFIle and sendProcess methods on the app class which might be enough, but if that's not good enough you can take advantage of the fact that the new version uses GuzzleHttp for responses and just do $app->response->setBody(new \GuzzleHttp\Stream\Stream($fp)); with any of the GuzzleHttp streaming classes.

    I don't know how to stream responses on the stable release(2.4.3). I don't think you can in any reasonable way. \Slim\Slim just echo's the finalized body as part of the response handling in Slim.php.

    I haven't tested any of this yet myself.

    Guzzle Docs:
    http://docs.guzzlephp.org/en/latest/streams.html#creating-streams

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac