0
votes

I am confused about this question and I would be greatful if someone could give me an explanation with concrete examples. I generated a CRUD controller with Symfony and also implemented FOSRestBundle Controller for REST. They both return the same data and I am wondering, what is the difference and what can one do that the other cannot? I would like to stick with only one at this point in a prototype I am creating and expand as soon as I understand Symfony2's ways of doing things more. Here is the CRUD code from Symfony2:

/**
 * Lists all User entities.
 *
 * @Route("/", name="user")
 * @Method("GET")
 * @Template()
 */
public function indexAction()
{
    $em = $this->getDoctrine()->getManager();

    $entities = $em->getRepository('SomethingWebServicesBundle:User')->findAll();

    return array(
        'entities' => $entities,
    );
}

By changing this method name, I get a FOSRestController (with the configuration done correctly)

// "get_users"     [GET] /users

public function getUsersAction()
{
    $em = $this->getDoctrine()->getManager();

    $entities = $em->getRepository('SomethingWebServicesBundle:Users')->findAll();

    return array(
        'entities' => $entities,
    );
}
1

1 Answers

1
votes

They are both controllers and you should use only one for the same tasks.

The default symfony controller is just a class with convience methods to get easy access to the most commonly needed things (for instance, creating a form, redirecting, etc.). You don't have to extend this controller, it just gives you some "extras".

The FOSRestController extends the Symfony controller and adds some convience methods when using the FOSRestBundle. Again, you don't have to use it, it just simplifies the code for you.

So that being said, the controllers don't do anything special. You can completely skip them and use your own stuff instead, a lot of people also prefer to not extend from the base controller, as it implies the Service Locator anti-pattern.

To get answers on what the FOSRestBundle can do, you should read their docs