Closed
Description
Q | A |
---|---|
Bug report? | no |
Feature request? | yes |
BC Break report? | no |
RFC? | no |
Symfony version | 3.1 |
Symfony don't officially recommends that controllers should be defined as a service. Therefore, some devs (like me) like to define the controllers as a service to keep them thin and not depending on container. This issue is to suggest some traits that make the same tasks of Symfony\Bundle\FrameworkBundle\Controller\Controller
, example:
trait FormTrait
{
private $formFactory;
/**
* Creates and returns a Form instance from the type of the form.
*
* @param string $type The fully qualified class name of the form type
* @param mixed $data The initial data for the form
* @param array $options Options for the form
*
* @return Form
*/
protected function createForm($type, $data = null, array $options = array())
{
return $this->formFactory->create($type, $data, $options);
}
/**
* Creates and returns a form builder instance.
*
* @param mixed $data The initial data for the form
* @param array $options Options for the form
*
* @return FormBuilder
*/
protected function createFormBuilder($data = null, array $options = array())
{
return $this->formFactory->createBuilder(FormType::class, $data, $options);
}
}
In that way I need only to inject the FormFactoryInterface
in the controller and I have the same shortcuts of Symfony\Bundle\FrameworkBundle\Controller\Controller
. The above example shows the trait for FormFactoryInterface
but it would be for the must used services like EngineInterface
, UrlGenerator
, Doctrine
, Security
and etc.