Archives by date

You are browsing the site archives by date.

【Symfony2.3】FOSRestBundleの使い方メモ。

シンプルにエンティティをjsonで出力する方法。

namespace Hoge\FugaBundle\Controller;

use Symfony\Component\HttpFoundation\Request;

use Hoge\FugaBundle\Entity\Post;

use FOS\RestBundle\Controller\FOSRestController;

/**
 * Post controller.
 */
class PostController extends FOSRestController
{

    /**
     * Lists all Post entities.
     */
    public function indexAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();
        
        $entities = $em->getRepository('HogeFugaBundle:Post')->findAll();
        
        $view = $this->view($entities, 200)->setFormat('json');
        
        return $this->handleView($view);
    }

}

色々ハマったけどこれだけでOK。