This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

sfActions : renderJson

Requests for new symfony features.

sfActions : renderJson

by jaymoo » Fri Jun 05, 2009 3:04 am

Hey would be nice to have this as a default rendering method in the sfActions class:

Code: Select all
 
public function renderJson($json)
{
  $this->getResponse()->setHttpHeader('Content-Type','application/json; charset=utf-8');
  return $this->renderText($json);
}
jaymoo
Junior Member
 
Posts: 6
Joined: Wed May 06, 2009 8:12 am
Location: Los Angeles, CA

Re: sfActions : renderJson

by Russ » Thu Jun 18, 2009 12:38 am

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

Code: Select all
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*
Image
User avatar
Russ
Faithful Member
 
Posts: 426
Joined: Mon Aug 04, 2008 11:04 am
Location: Oslo, Norway

Re: sfActions : renderJson

by jaymoo » Thu Jun 18, 2009 1:12 am

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
jaymoo
Junior Member
 
Posts: 6
Joined: Wed May 06, 2009 8:12 am
Location: Los Angeles, CA

Re: sfActions : renderJson

by Russ » Thu Jun 18, 2009 11:42 am

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*
Image
User avatar
Russ
Faithful Member
 
Posts: 426
Joined: Mon Aug 04, 2008 11:04 am
Location: Oslo, Norway

Re: sfActions : renderJson

by Urkman » Fri Jul 10, 2009 2:56 pm

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
Urkman
Junior Member
 
Posts: 12
Joined: Wed Jul 08, 2009 11:54 am

Re: sfActions : renderJson

by jmiridis » Sun Jan 08, 2012 8:25 pm

You could extend the sfAction Component like this:

Code: Select all
class frontendConfiguration extends sfApplicationConfiguration
{
  public function configure()
  {
    $this->dispatcher->connect('component.method_not_found', array('sfActionExtension', 'listenToMethodNotFound'));
  }
}

class sfActionExtension
{
  static public function listenToMethodNotFound(sfEvent $event)
  {
    switch ($event['method'])
    {
      case 'renderJson':
        $sfAction = $event->getSubject();
        $sfAction->getResponse()->setHttpHeader('Content-Type','application/json;charset=utf-8');
        $event->setReturnValue($sfAction->renderText(json_encode($event['arguments'][0])));
        return true;
      default:
        return false;
    }
  }
}
jmiridis
Junior Member
 
Posts: 20
Joined: Sun Apr 12, 2009 5:32 pm

Re: sfActions : renderJson

by Russ » Mon Jan 09, 2012 3:01 pm

Urkman wrote: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


If you already have a JSON formatted string then you simply need to do:

Code: Select all
return $this->renderText($myString);
*On Freenode I am rooster*
Image
User avatar
Russ
Faithful Member
 
Posts: 426
Joined: Mon Aug 04, 2008 11:04 am
Location: Oslo, Norway