10

I am using symfony 1.4, to create my project with propel as ORM. i want to get the response in JSON format, when i call a url. I have set the headers to "application/json" but it is not working, i am getting the response back in HTML format..which i am not able to decode. How can we set content-type in symfony?? example code: Action-

 public function executeIndex(sfWebRequest $request)
 {
     $data_array=array("name" => "harry", "mobile" => "9876543210");
     $data_json=json_encode($data_array);
     $this->output=$data_json;  
 }

View-

<?php
  header('Content-type: application/json');
  echo $output;
?>
Harish Kurup
  • 7,257
  • 19
  • 65
  • 94
  • you don't have to set only the content-type, but additionally the framework has to send out the data as JSON istead of HTML... You're doing something like writing "CDROM" on a DVD and then stick it into a CDROM-only device – joni Oct 26 '10 at 06:37
  • ok..like how can i set data as JSON, i an getting an array from database and i converted it in to JSON format then echo'ed it through template by setting the header content-type to application/json.. – Harish Kurup Oct 26 '10 at 06:43

4 Answers4

20

ok i got where i was going wrong..yhe code should have been.. Action-

public function executeIndex(sfWebRequest $request)
{
 $this->getResponse()->setContentType('application/json');
 $data_array=array("name" => "harry", "mobile" => "9876543210");
 $data_json=json_encode($data_array);
 return $this->renderText($data_json); 
}

this code has worked for me, please post if any better solution you have got..

Harish Kurup
  • 7,257
  • 19
  • 65
  • 94
  • 3
    You don't need to have anything in the view template if you're using $this->renderText(). – richsage Oct 26 '10 at 07:57
  • ok! i.e no need to echo $data_json; yeah the render function will any way render and throw the JSON output.. hey thank you @richsage – Harish Kurup Oct 26 '10 at 08:32
6

You can also define this in the view.yml (apps/{app_name}/modules/{module_name}/config/view.yml) file for the module.

indexSuccess:
  has_layout: false
  http_metas:
    content-type: application/json
Dan Morphis
  • 1,708
  • 19
  • 23
  • yeah that great, we can get rid of $this->getResponse()->setContentType('application/json'). all time its been a problem.. I have not tried this but Thank You! i will do it in the future projects. – Harish Kurup May 26 '11 at 06:52
  • 1
    @Harish Kurup Your welcome. I actually came across this post while trying to figure it out myself. But I kept thinking, with all the awesomeness that is in the Symfony framework, there has to be a way to set the Content-Type from a yml file. The answer was buried and non-descript so I decided to try it and was pleasantly surprised when it worked! Msg me if you have any issues getting it to work. – Dan Morphis May 26 '11 at 18:57
3

Oh ! As an alternative, I'd just say I have been using the routing system, which provides a pretty neat way to get it done:

-> In your routing.yml

json_test:
  url: /test
  class: sfRequestRoute
  param: { module: test, action: index, sf_format: json }

Then, the frameworks will automatically pick up you view index.json.php, which you have to create. As mentioned above, you can generate the content in the action with json_encode, though there are arguments to put it in the view.

Now... Ok, if you are interested in picking some more about this, have a look at the "Practical symfony" tutorial: Day 15: Web Services There's some good stuff down there !

mika
  • 1,971
  • 3
  • 18
  • 32
1

In a REST style, just add .format to the URI, create the relative template and Symfony Routing system does the rest of the work for us.

pagaio
  • 11
  • 1