-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Updates to DI config for 3.3 #7807
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
Changes from 7 commits
8433fc1
2d11347
105801c
049df7d
9e84572
c45daf4
2636bea
9ab27f0
0e48bd8
6e6ed94
70178d1
45500b3
759e9b2
6de83e2
89e12de
443aec2
bc7088d
ee27765
5452c61
2229fd3
cac3c6c
12c4944
22adfbd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,6 +148,11 @@ that's available to you with or without the use of the base | |
action is to look in the | ||
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller` class. | ||
|
||
.. tip:: | ||
If you know what you're doing, you can alternatively extend | ||
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController`. It | ||
has all the same shortcuts, but does not have a ```$this->container`` property. | ||
|
||
.. index:: | ||
single: Controller; Redirecting | ||
|
||
|
@@ -236,12 +241,11 @@ The Symfony templating system and Twig are explained more in the | |
Accessing other Services | ||
~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Symfony comes packed with a lot of useful objects, called *services*. These | ||
are used for rendering templates, sending emails, querying the database and | ||
any other "work" you can think of. When you install a new bundle, it probably | ||
brings in even *more* services. | ||
Symfony comes packed with a lot of useful objects, called :doc:`services </service_container>`. | ||
These are used for rendering templates, sending emails, querying the database and | ||
any other "work" you can think of. | ||
|
||
When extending the base controller class, you can access any Symfony service | ||
When extending the base ``Controller`` class, you can access any Symfony service | ||
via the :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::get` | ||
method of the ``Controller`` class. Here are several common services you might | ||
need:: | ||
|
@@ -252,6 +256,9 @@ need:: | |
|
||
$mailer = $this->get('mailer'); | ||
|
||
// you can also fetch parameters | ||
$someParameter = $this->getParameter('some_parameter'); | ||
|
||
What other services exist? To list all services, use the ``debug:container`` | ||
console command: | ||
|
||
|
@@ -261,14 +268,31 @@ console command: | |
|
||
For more information, see the :doc:`/service_container` article. | ||
|
||
.. tip:: | ||
Services as Controller Arguments | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
You can also tell Symfony to pass your a service as a controller argument by type-hinting | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "you" (not "your") |
||
it:: | ||
|
||
To get a :ref:`container configuration parameter <config-parameter-intro>`, | ||
use the | ||
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::getParameter` | ||
method:: | ||
use Psr\Log\LoggerInterface | ||
// ... | ||
|
||
/** | ||
* @Route("/lucky/number/{max}") | ||
*/ | ||
public function numberAction($max, LoggerInterface $logger) | ||
{ | ||
$logger->info('We are logging!'); | ||
|
||
$from = $this->getParameter('app.mailer.from'); | ||
// ... | ||
} | ||
|
||
.. note:: | ||
If this isn't working, make sure your controller is registered as a service, | ||
:ref:`autoconfigured <services-autoconfigure>` and extends either | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't |
||
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller` or | ||
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController`. Or, | ||
you can tag it manually with ``controller.service_arguments``. | ||
|
||
.. index:: | ||
single: Controller; Managing errors | ||
|
@@ -407,20 +431,6 @@ For example, imagine you're processing a :doc:`form </forms>` submission:: | |
return $this->render(...); | ||
} | ||
|
||
.. tip:: | ||
|
||
As a developer, you might prefer not to extend the ``Controller``. To | ||
use the flash message functionality, you can request the flash bag from | ||
the :class:`Symfony\\Component\\HttpFoundation\\Session\\Session`:: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this also be removed in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to re-read and re-think that article in its entirety... since controllers as services are a first-class citizen (does it make sense to still have a separate article?) |
||
|
||
use Symfony\Component\HttpFoundation\Session\Session; | ||
|
||
public function indexAction(Session $session) | ||
{ | ||
// getFlashBag is not available in the SessionInterface and requires the Session | ||
$flashBag = $session->getFlashBag(); | ||
} | ||
|
||
After processing the request, the controller sets a flash message in the session | ||
and then redirects. The message key (``notice`` in this example) can be anything: | ||
you'll use this key to retrieve the message. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -548,6 +548,7 @@ a controller, this is pretty easy. Add the following method to the | |
// ... | ||
use AppBundle\Entity\Product; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
|
||
// ... | ||
public function createAction() | ||
|
@@ -568,6 +569,12 @@ a controller, this is pretty easy. Add the following method to the | |
return new Response('Saved new product with id '.$product->getId()); | ||
} | ||
|
||
// you can also receive the $em as an argument | ||
public function editAction(EntityManagerInterface $em) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we also add this to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll update the whole Doctrine section in another PR with more of this fancy type-based stuff :) |
||
{ | ||
// ... | ||
} | ||
|
||
.. note:: | ||
|
||
If you're following along with this example, you'll need to create a | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,7 +100,7 @@ The Swift Mailer library works by creating, configuring and then sending | |
of the message and is accessible via the ``mailer`` service. Overall, sending | ||
an email is pretty straightforward:: | ||
|
||
public function indexAction($name) | ||
public function indexAction($name, \Swift_Mailer $mailer) | ||
{ | ||
$message = \Swift_Message::newInstance() | ||
->setSubject('Hello Email') | ||
|
@@ -125,7 +125,11 @@ an email is pretty straightforward:: | |
) | ||
*/ | ||
; | ||
$this->get('mailer')->send($message); | ||
|
||
$mailer->send($message); | ||
|
||
// or, you can also fetch the mailer service in this way | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. imo we should remove the |
||
// $this->get('mailer')->send($message); | ||
|
||
return $this->render(...); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
~~If you know what you're doing, ~~ : this just creates fear to me, whereas the diff is explained very simply in the next sentence, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just add that
$this->get()
is gone also?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would reuse the wording of #7657: