You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 */protectedfunctioncreateForm($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 */protectedfunctioncreateFormBuilder($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.
The text was updated successfully, but these errors were encountered:
javiereguiluz
changed the title
Create treats for common tasks in controller
Create traits for common tasks in controller
Mar 20, 2017
Regarding the EngineInterface, I've proposed an idea with lazy rendering (replacing @Template), which someone turned into a PR #21765. This should effectively replace 1 dependency.
Your forms can have a lot of dependencies:
Form Factory
Usually a repository to fetch current data
The Entity Manager (or repository) to persist the new data
A url generator to redirect
A flash bag/session to store a message that it went fine
Any other dependency like an API or what ever to handle forms
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:In that way I need only to inject the
FormFactoryInterface
in the controller and I have the same shortcuts ofSymfony\Bundle\FrameworkBundle\Controller\Controller
. The above example shows the trait forFormFactoryInterface
but it would be for the must used services likeEngineInterface
,UrlGenerator
,Doctrine
,Security
and etc.The text was updated successfully, but these errors were encountered: