Skip to content

Instantly share code, notes, and snippets.

@havvg
Created July 9, 2012 20:09
Show Gist options
  • Save havvg/3078597 to your computer and use it in GitHub Desktop.
Save havvg/3078597 to your computer and use it in GitHub Desktop.
Symfony2 Propel API response
<?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);
}
}
{% 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 %}
<?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,
));
}
}
shipdev_hull_list:
pattern: /hulls/{id}.{_format}
defaults: { _controller: ShipdevBundle:Hull:list }
requirements:
id: \d+
_method: GET
_format: json|html
@havvg
Copy link
Author

havvg commented Jul 9, 2012

Example response, Backbone.JS compatible :)

[
    {
        "id": 1,
        "name": "Korvette",
        "set_id": 1,
        "order": 1,
        "min_level": 1,
        "max_level": 5
    },
    {
        "id": 2,
        "name": "Fregatte",
        "set_id": 1,
        "order": 2,
        "min_level": 2,
        "max_level": 33
    },
    {
        "id": 3,
        "name": "Frachter",
        "set_id": 1,
        "order": 3,
        "min_level": 3,
        "max_level": 123
    },
    {
        "id": 4,
        "name": "Tankschiff",
        "set_id": 1,
        "order": 4,
        "min_level": 5,
        "max_level": 126
    },
    {
        "id": 5,
        "name": "Versorgungsschiff",
        "set_id": 1,
        "order": 5,
        "min_level": 7,
        "max_level": 121
    },
    {
        "id": 6,
        "name": "Zerstörer",
        "set_id": 1,
        "order": 6,
        "min_level": 8,
        "max_level": 55
    },
    {
        "id": 7,
        "name": "Transportschiff",
        "set_id": 1,
        "order": 7,
        "min_level": 12,
        "max_level": 121
    },
    {
        "id": 8,
        "name": "Begleitschiff",
        "set_id": 1,
        "order": 8,
        "min_level": 13,
        "max_level": 121
    },
    {
        "id": 9,
        "name": "Kommandoschiff",
        "set_id": 1,
        "order": 9,
        "min_level": 14,
        "max_level": 121
    },
    {
        "id": 10,
        "name": "Waffenplattform",
        "set_id": 1,
        "order": 10,
        "min_level": 21,
        "max_level": 50
    },
    {
        "id": 11,
        "name": "Kreuzer",
        "set_id": 1,
        "order": 11,
        "min_level": 50,
        "max_level": 61
    },
    {
        "id": 12,
        "name": "Gefechtsstation",
        "set_id": 1,
        "order": 12,
        "min_level": 75,
        "max_level": 71
    },
    {
        "id": 13,
        "name": "Schlachtkreuzer",
        "set_id": 1,
        "order": 13,
        "min_level": 80,
        "max_level": 82
    }
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment