8000 Create traits for common tasks in controller · Issue #22061 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Create traits for common tasks in controller #22061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tsantos84 opened this issue Mar 20, 2017 · 3 comments
Closed

Create traits for common tasks in controller #22061

tsantos84 opened this issue Mar 20, 2017 · 3 comments

Comments

@tsantos84
Copy link
Contributor
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.

@javiereguiluz javiereguiluz changed the title Create treats for common tasks in controller Create traits for common tasks in controller Mar 20, 2017
@linaori
Copy link
Contributor
linaori commented 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

To reduce the controller size, you can also use this package: https://github.com/hostnet/form-handler-bundle

Effectively this reduces the amount of dependencies in your controller quite some and aims for slim controllers.

Note that there's an open PR that will improve quite some, reducing dependencies of your action. This PR should be merged soon.

Regarding the other dependencies:

  • Security: use the @Security annotation
  • Doctrine: use (and inject) repositories if you need them

@GeoffreyHervet
Copy link

You're maybe looking for a FormHelper instead of a trait ?

@nicolas-grekas
Copy link
Member

Instead of a new trait, this should be proposed as new methods on ControllerTrait, see #18193

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants
0