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

Slim response json

Hugues's Avatar

Hugues

24 Mar, 2012 02:16 AM

Slim is great, but there is something I don't understand.

Right now when my route is called, I use :

Slim::getInstance()->response()->header('Content-Type', 'application/json');
Slim::getInstance()->response()->body(json_encode( $result)));

I could also do :
header('Content-Type: application/json');
echo json_encode( $result);

but it's kind of lame, no ? I should be able to do something like :
Slim::getInstance()->response()->contentType('application/json');
Slim::getInstance()->response()->result($result);
And Slim should detect that he have to encode using json ?
Or better grab that for request header and act accordingly ?

Am I missing something here ?

  1. 1 Posted by uwe.d.wagner on 24 Mar, 2012 11:48 AM

    uwe.d.wagner's Avatar

    Try this:

    $app = Slim::getInstance();
    $app->contentType('application/json');
    echo '{"something": ' . json_encode($result) . '}';
    

    or use a template with $app->render('')

  2. 2 Posted by Hugues on 24 Mar, 2012 08:27 PM

    Hugues's Avatar

    @Uwe, yeah thx for your answer, i tried using render with a json formatting template (receveive a result as data and do echo json_encode($data)), but my point is result encoding shouln't be the concern of the app logic, it should be the RESTFul layer responsability, no ?

  3. 3 Posted by uwe.d.wagner on 25 Mar, 2012 10:38 AM

    uwe.d.wagner's Avatar

    Oh, you are talking about content negotiation.
    By the way there is another thread who adresses the subject: http://help.slimframework.com/discussions/questions/55-support-exte... who

  4. 4 Posted by hugues on 26 Mar, 2012 04:53 PM

    hugues's Avatar

    thx man, not happy with this solution either. i guess custom route after dispatch with custom view is my solution.

  5. Support Staff 5 Posted by Josh Lockhart on 26 Mar, 2012 06:13 PM

    Josh Lockhart's Avatar

    Content negotiation is in the works. No ETA yet.

  6. 6 Posted by hugues on 26 Mar, 2012 08:40 PM

    hugues's Avatar

    @josh, thx !

  7. Support Staff 7 Posted by Josh Lockhart on 26 Mar, 2012 08:41 PM

    Josh Lockhart's Avatar

    Cool. Closing this out.

  8. Josh Lockhart closed this discussion on 26 Mar, 2012 08:41 PM.

Comments are currently closed for this discussion. You can start a new one.

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