Create an API for the todo tasks

Task entity

Make sure you have configured your database correctly in app/config/parameters.yml. Create a Task entity and run doctrine:schema:update --force in order to update the database schema.

src/AppBundle/Entity/Task.php
<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Validator;

/**
 * @ORM\Entity()
 * @ORM\Table(name="task")
 */
class Task
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     *
     * @var int
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     *
     * @var string
     */
    protected $text;

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return string
     *
     * @Validator\NotNull()
     */
    public function getText()
    {
        return $this->text;
    }

    /**
     * @param string $text
     */
    public function setText($text)
    {
        $this->text = $text;
    }
}

Task controller

Our task controller uses method names that gets recognized by FosRestBundle. The controller methods gets automatically generated route names based on their prefixes. The complete list of prefixes is available here.

src/AppBundle/Controller/TaskController.php
<?php

namespace AppBundle\Controller;

use AppBundle\Entity\Task;
use FOS\RestBundle\Controller\FOSRestController;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class TaskController extends FOSRestController
{
    /**
     * @ParamConverter()
     * @ApiDoc()
     *
     * @param Task $task
     *
     * @return Task
     */
    public function getTaskAction(Task $task)
    {
        return $task;
    }

    /**
     * @ApiDoc()
     *
     * @return Task[]
     */
    public function getTasksAction()
    {
        return $this->getDoctrine()->getRepository('AppBundle:Task')->findAll();
    }

    /**
     * @ApiDoc()
     *
     * @param Request $request
     *
     * @return Task
     */
    public function postTasksAction(Request $request)
    {
        $task = new Task();
        $task->setText($request->request->get('text'));

        $errors = $this->get('validator')->validate($task);
        if (count($errors) > 0) {
            $errorStrings = [];
            foreach ($errors as $error) {
                $errorStrings[] = $error->getMessage();
            }
            return $this->view(
                [
                    'error' => implode(',', $errorStrings)
                ],
                Response::HTTP_BAD_REQUEST
            );
        }

        $this->getDoctrine()->getManager()->persist($task);
        $this->getDoctrine()->getManager()->flush();

        return $task;
    }

    /**
     * @ApiDoc()
     *
     * @param Task $task
     *
     * @return Response
     */
    public function deleteTaskAction(Task $task)
    {
        $this->getDoctrine()->getManager()->remove($task);
        $this->getDoctrine()->getManager()->flush();

        return new Response('', Response::HTTP_NO_CONTENT);
    }
}

Configure the routes

By adding the following lines to routing.yml we tell FosRestBundle to look for methods in TaskController.php.

app/config/routing.yml
// ...

tasks:
    resource: "@AppBundle/Controller/TaskController.php"
    type:     rest
    prefix: /api