Skip to content

Mention the getParameter() helper in the configuration article #12333

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

Merged
merged 1 commit into from
Sep 18, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <configuration-accessing-parameters>`.

.. index::
single: Environments; Introduction
Expand Down Expand Up @@ -642,8 +642,10 @@ the env files ending in ``.local`` (``.env.local`` and ``.env.<environment>.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 <configuration-parameters>`
Expand All @@ -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 </service_container/autowiring>` doesn't work for
parameters. Instead, inject them explicitly:
In controllers extending from the :ref:`AbstractController <the-base-controller-class-services>`,
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 </service_container/autowiring>`
doesn't work for parameters:

.. configuration-block::

Expand Down