Skip to content

Commit

Permalink
minor symfony#7495 Improved the advantages/drawbacks of "controllers …
Browse files Browse the repository at this point in the history
…as services" (javiereguiluz)

This PR was merged into the 2.7 branch.

Discussion
----------

Improved the advantages/drawbacks of "controllers as services"

This fixes symfony#6749.

Commits
-------

e8b9e62 Improved the advantages/drawbacks of "controllers as services"
  • Loading branch information
xabbuh committed Jul 21, 2017
2 parents dcde03c + e8b9e62 commit 096046c
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions controller/service.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@ These are the main **advantages** of defining controllers as services:
service container configuration. This is useful when developing reusable bundles;
* Your controllers are more "sandboxed". By looking at the constructor arguments,
it's easy to see what types of things this controller may or may not do;
* If you're not passing some required dependencies or if you are injecting some
non-existent services, you'll get errors during the container compilation
instead of during runtime execution;
* Since dependencies must be injected manually, it's more obvious when your
controller is becoming too big (i.e. if you have many constructor arguments).

These are the main **drawbacks** of defining controllers as services:

* It takes more work to create the controllers because they don't have
automatic access to the services or to the base controller shortcuts;
* It takes more work to create the controllers and they become more verbose
because they don't have automatic access to the services and the base
controller shortcuts;
* The constructor of the controllers can rapidly become too complex because you
must inject every single dependency needed by them;
* The code of the controllers is more verbose because you can't use the shortcuts
of the base controller and you must replace them with some lines of code.
must inject every single dependency needed by them.

The recommendation from the :doc:`best practices </best_practices/controllers>`
is also valid for controllers defined as services: avoid putting your business
Expand Down

0 comments on commit 096046c

Please sign in to comment.