Created
July 9, 2012 20:09
-
-
Save havvg/3078597 to your computer and use it in GitHub Desktop.
Symfony2 Propel API response
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Sternenbund\Bundle\ApplicationBundle\Controller; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller as BaseController; | |
use PropelObjectCollection; | |
abstract class AbstractController extends BaseController | |
{ | |
// even more little helpers | |
/** | |
* Returns a JSON Response. | |
* | |
* @param array $parameters | |
* @param Response|null $response | |
* | |
* @return Response | |
*/ | |
public function renderJson(array $parameters = array(), Response $response = null) | |
{ | |
return $this->render('::base.json.twig', $parameters, $response); | |
} | |
/** | |
* Returns a JSON Response containing the collection entries as data. | |
* | |
* @param PropelObjectCollection $coll | |
* @param Response|null $response | |
* | |
* @return Response | |
*/ | |
public function renderJsonCollection(PropelObjectCollection $coll, Response $response = null) | |
{ | |
return $this->renderJson(array('data' => $coll->toArray(null, false, \BasePeer::TYPE_FIELDNAME)), $response); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% set jsonp = app.request.query.get('jsonp') %} | |
{% if jsonp %}{{ jsonp }}({% endif %} | |
{% block data %}{% if is_json|default(false) %}{{ data|raw }}{% else %}{{ data|json_encode|raw }}{% endif %}{% endblock %} | |
{% if jsonp %});{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Sternenbund\Bundle\ShipdevBundle\Controller; | |
use Sternenbund\Bundle\ApplicationBundle\Controller\AbstractController; | |
use Sternenbund\Bundle\ShipdevBundle\Form\Type\HullType; | |
use Sternenbund\Bundle\ShipdevBundle\Model\Hull\Hull; | |
use Sternenbund\Bundle\ShipdevBundle\Model\Hull\HullPeer; | |
use Sternenbund\Bundle\ShipdevBundle\Model\Hull\HullQuery; | |
use Sternenbund\Bundle\ShipdevBundle\Model\Shipdev\Set; | |
use Symfony\Component\Form\FormError; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
class HullController extends AbstractController | |
{ | |
// more actions ... | |
public function listAction(Set $set = null) | |
{ | |
$query = HullQuery::create(); | |
if ($set) { | |
$query->filterBySet($set); | |
} | |
/* @var $hulls \PropelObjectCollection */ | |
$hulls = $query | |
->orderByOrder() | |
->addAscendingOrderByColumn(HullPeer::NAME) | |
->find() | |
; | |
if ('json' === $this->getRequest()->getRequestFormat()) { | |
return $this->renderJsonCollection($hulls); | |
} | |
return $this->render('ShipdevBundle:Hull:list.html.twig', array( | |
'hulls' => $hulls, | |
)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shipdev_hull_list: | |
pattern: /hulls/{id}.{_format} | |
defaults: { _controller: ShipdevBundle:Hull:list } | |
requirements: | |
id: \d+ | |
_method: GET | |
_format: json|html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example response, Backbone.JS compatible :)