18

In symfony, I call an action and I want this to return json to jQuery frontend.

The Jobeet tutorial teaches how to return a partial but I want to return json, not a partial.

4 Answers 4

38

If it's just a normal AJAX action you're returning it from, I think I've used the following somewhere in the past:

return $this->renderText(json_encode($something));
Sign up to request clarification or add additional context in comments.

2 Comments

If you have the web debugging bar active in dev mode, you must add the line "sfConfig::set('sf_web_debug',false);"
This will return json in the response body, but the content type will be text/html. Add $this->getResponse()->setContentType('application/json');
17

The cheap way:

function executeSomethingThatReturnsJson(){
  $M = new Model();
  $stuff = $M->getStuff();
  echo json_encode($stuff);
  die(); //don't do any view stuff
}

The smarter way:

A smarter way is to create a nice subclass of sfActions that helps handling json-stuff.

In a project I did recently, I created a application called 'api' (./symfony generate:application api)

and then created a file like:

api/lib/apiActions.class.php

<?PHP
class apiActions extends sfActions {
  public function returnJson($data){
    $this->data = $data;
    if (sfConfig::get('sf_environment') == 'dev' && !$this->getRequest()->isXmlHttpRequest()){
      $this->setLayout('json_debug'); 
      $this->setTemplate('json_debug','main');
    }else{
      $this->getResponse()->setHttpHeader('Content-type','application/json');
      $this->setLayout('json');
      $this->setTemplate('json','main');
    }
  } 
}

Notice that I explicitly set the template there.

So my jsonSuccess.php template is simply:

<?PHP echo json_encode($data);

While json_debugSuccess.php makes things prettier:

<?PHP var_dump($data); ?>

Then you can have a controller that extends apiActions (instead of the usual sfActions) that looks like this:

<?php
class myActions extends apiAction {
  public function executeList(sfWebRequest $request)
  {
    $params = array();
    if ($request->hasParameter('id')){
      $id = $request->getParameter('id');
      if (is_numeric($id)){
        $params['id'] = $id;
      }
    }
    $data = Doctrine::getTable('SomeTable')->findAll();
    $this->returnJson($data);
  }
}

Disclaimer: The code above is copy/pasted out of an app I have, but simplified. It's for illustrative purposes only -- but it should get you heading in the right direction.

2 Comments

this was really funny :) u posted a lot of codes but the previous answer was one line and it worked :) 1+ for u being eager to help though!:)
Right -- that other answer is a nicer version of "the cheap way". The rest of my answer is about handling things differently based on environment, though I'm sure it could be done a lot cleaner.
4

FYI: In case of Symfony 2.x "quick and dirty" way looks like this:

return  new Response(json_encode($data), 200, array('Content-Type', 'text/json'));

1 Comment

I think you can even just put Response(json_encode($data)) if your route specifies the good format (defaults: { _controller: your:action, _format: json })
0

Return new JsonResponse(array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.