Search code examples
formssymfonycontroller

How can I reduce using extension types in Symfony?


In my Controller I am using a lot of extension Types:

use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

Because this takes so much space and is for me unclear, I need to know if there is one line to use just all lines. Something like this:

use Symfony\Component\Form\Extension\Core\Type\AllTypes;

Solution

  • Each of the classes do something specific, and so using them can't be avoided.

    However, I do like to drop down the namespace, to help make clear what things are (though it's less necessary here, as they are all suffixed). It does reduce a lot of the bulk in the use statements as well. I do find it quite useful where I may have several classes with the same class name, but in different parts of the codebase - maybe an Entity versus some kind of related service.

    <?php
    use Symfony\Component\Form\Extension\Core\Type; 
    
    ->add('hiddenfieldname', Type\HiddenType::class, [...])
    ->add('password', Type\RepeatedType::class, ....