This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » development » Feature requests » sfActions : renderJson
sfActions : renderJson [message #79433] Fri, 05 June 2009 03:04 Go to next message
jaymoo  is currently offline jaymoo
Messages: 6
Registered: May 2009
Location: Los Angeles, CA
Junior Member
Hey would be nice to have this as a default rendering method in the sfActions class:

  
public function renderJson($json)
{
  $this->getResponse()->setHttpHeader('Content-Type','application/json; charset=utf-8');
  return $this->renderText($json);
}
Re: sfActions : renderJson [message #80074 is a reply to message #79433 ] Thu, 18 June 2009 00:38 Go to previous messageGo to next message
Russ  is currently offline Russ
Messages: 413
Registered: August 2008
Location: Oslo, Norway
Faithful Member

Since json is a headers-only response (no response body) it would have to be something like this I believe:

public function renderJson($values, $statusCode = 200)
{
  $output = json_encode($values);
  $this->getResponse()->setStatusCode($statusCode);
  $this->getResponse()->setHttpHeader("X-JSON", '('.$output.')');
  return sfView::HEADER_ONLY;
}


*On Freenode I am rooster*
http://www.walkandclimb.co.uk/images/monkeydust.jpghttp://www.walkandclimb.co.uk/userpix/2_linux_wave_small_2.jpg
Re: sfActions : renderJson [message #80078 is a reply to message #80074 ] Thu, 18 June 2009 01:12 Go to previous messageGo to next message
jaymoo  is currently offline jaymoo
Messages: 6
Registered: May 2009
Location: Los Angeles, CA
Junior Member
Hey yeah I came across that method but it's discouraged since the header has a size limit and some browsers don't do well with an empty body.

http://www.symfony-project.org/book/1_2/11-Ajax-Integration
Re: sfActions : renderJson [message #80108 is a reply to message #80078 ] Thu, 18 June 2009 11:42 Go to previous messageGo to next message
Russ  is currently offline Russ
Messages: 413
Registered: August 2008
Location: Oslo, Norway
Faithful Member

Interesting, maybe it could be optional.

I must admit I've used the header only approach a lot and never come across any problems, but then it's only normally for very small responses like "success" => true or a simple message.

It also makes the parsed json available automatically via Prototype, so you don't have to eval the response or anything like that.

I do agree with you though - it seems that using the response body is the best way to go for nearly all cases.


*On Freenode I am rooster*
http://www.walkandclimb.co.uk/images/monkeydust.jpghttp://www.walkandclimb.co.uk/userpix/2_linux_wave_small_2.jpg
Re: sfActions : renderJson [message #81574 is a reply to message #79433 ] Fri, 10 July 2009 14:56 Go to previous message
Urkman  is currently offline Urkman
Messages: 10
Registered: July 2009
Junior Member
Hello,

I found this thread so I add my question here Smile
I also started a thread in the 1.2 forum:
http://forum.symfony-project.org/index.php/t/21718/

What would be the best way to just return a json formated string, without parsing a template?

Thanks for your help,
Urkman
Previous Topic:symfony container
Next Topic:sfpager::getPage();
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software