From d8e43b998b974aee6f9988e421474d77a4d91204 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 17 Sep 2019 17:35:08 +0200 Subject: [PATCH] Mention the getParameter() helper in the configuration article --- configuration.rst | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/configuration.rst b/configuration.rst index 77e770f0181..b163fc4ba72 100644 --- a/configuration.rst +++ b/configuration.rst @@ -320,8 +320,8 @@ a new ``locale`` parameter is added to the ``config/services.yaml`` file). .. seealso:: - Read the `Accessing Configuration Values`_ section of this article to learn - about how to use these configuration parameters in services and controllers. + Later in this article you can read how to + ref:`get configuration parameters in controllers and services `. .. index:: single: Environments; Introduction @@ -642,8 +642,10 @@ the env files ending in ``.local`` (``.env.local`` and ``.env..loca involving a ``.env.dist`` file. For information about upgrading, see: :doc:`configuration/dot-env-changes`. -Accessing Configuration Values ------------------------------- +.. _configuration-accessing-parameters: + +Accessing Configuration Parameters +---------------------------------- Controllers and services can access all the configuration parameters. This includes both the :ref:`parameters defined by yourself ` @@ -654,9 +656,31 @@ all the parameters that exist in your application: $ php bin/console debug:container --parameters -Parameters are injected in services as arguments to their constructors. -:doc:`Service autowiring ` doesn't work for -parameters. Instead, inject them explicitly: +In controllers extending from the :ref:`AbstractController `, +use the ``getParameter()`` helper:: + + // src/Controller/UserController.php + namespace App\Controller; + + use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; + + class UserController extends AbstractController + { + // ... + + public function index() + { + $projectDir = $this->getParameter('kernel.project_dir'); + $adminEmail = $this->getParameter('app.admin_email'); + + // ... + } + } + +In services and controllers not extending from ``AbstractController``, inject +the parameters as arguments of their constructors. You must inject them +explicitly because :doc:`service autowiring ` +doesn't work for parameters: .. configuration-block::