NOTE: This is the first post of a new series called "Meet the bundle" dedicated to promote Symfony bundles developed by the community and which are not well-known yet.


Annotations are one of the favorite topics to engage in a discussion with other developers. Some of them think that using annotations is a bad practice full of drawbacks, while others love how annotations reduce a fair amount of boilerplate code in their applications.

As usual, Symfony position is friendly for both sides: if you don't like annotations, don't use them. If you like them, Symfony supports and provides you with several useful annotations via the SensioFrameworkExtraBundle.

In this article we'll introduce ControllerExtraBundle, which is a bundle that provides even more annotations to perform common controller tasks. One of those annotations is called Pagination and it allows you to create and inject paginator objects:

1
2
3
4
5
6
7
8
9
10
use Doctrine\ORM\Tools\Pagination\Paginator;
use Mmoreram\ControllerExtraBundle\Annotation\Paginator as CreatePaginator;

/**
 * @CreatePaginator(class = "AppBundle:Product")
 */
public function listAction(Paginator $paginator)
{
    // ...
}

This annotation defines a lot of configuration options: the number of elements per page, the default result page number, the properties used for ordering results, the conditions used to filter results, etc.

Another useful annotation is Form, which creates Symfony forms based on the type class or the type service id, as shown in the following example:

1
2
3
4
5
6
7
8
9
10
use Mmoreram\ControllerExtraBundle\Annotation\Form as CreateForm;
use Symfony\Component\Form\Form;

/**
 * @CreateForm(class = "user_type", name = "user")
 */
public function registerAction(Form $user)
{
    // ...
}

This annotation can even be combined with Symfony's @ParamConverter to create a form object with an previously created entity:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\Form\Form;
use Mmoreram\ControllerExtraBundle\Annotation\Form as CreateForm;
use AppBundle\Entity\User;

/**
 * @Route(path = "/user/{id}", name = "user_view")
 *
 * @ParamConverter("user", class="AppBundle:User")
 * @CreateForm(class = "user_type", entity = "user", name = "form")
 */
public function viewAction(User $user, Form $form)
{
}

Read the bundle's documentation to learn about the rest of annotations: @Entity @ObjectManager, @Flush, @JsonResponse, @Log, @Get and @Post.

About the author

ControllerExtraBundle is developed by Marc Morera, a Symfony veteran from Barcelona (Spain). In addition to leading the Symfony Barcelona group, he is the chief architect of Elcodi, an e-commerce platform developed with Symfony components.

We're looking for the next bundle

Do you know any other useful Symfony bundle which is not well-known by the community yet? Tell us about it in the comments below or send us an email to javier.eguiluz@sensiolabs.com and we'll feature it in the next article of this series!

Published in #Meet the Bundle