33

From symfony 4, I want create a global parameter and get the value of this parameter from a controller.

This parameter is the path of an another app in the server. So, I thinked add the paramerter in the .env file. But how can I get the value of this parameter from a controller?

7 Answers 7

57

For Symfony 5

in your .env file

APP_PARAM=paramvaluehere

in your config/services.yaml

parameters:
    app.paramname: '%env(APP_PARAM)%'

in your controller

$this->getParameter('app.paramname');
4
  • 1
    Works in 4.3 too.
    – learner
    May 7, 2021 at 12:58
  • 3
    You can simply do $_SERVER['APP_PARAM'] with value only in .env file symfony.com/doc/current/cloud/cookbooks/…
    – TikTaZ
    Sep 27, 2021 at 14:04
  • To be able to call $this->getParameter('app.paramname') make sure your controller extends AbstractController.
    – iloo
    Dec 1, 2021 at 14:27
  • The config/services.yaml part is only required if your controller DOES NOT extends AbstractController. symfony.com/doc/current/…
    – Jibato
    Jul 13, 2022 at 9:47
38

If someone is stil looking for a quick fix but not much symfonish,

define your parameter in .env

MY_PARAM='my param value'

and then in controller call it

echo $_ENV['MY_PARAM'];

if you call the varaible in the controller you better define it in the config/services.yaml under parameters section and access it through parameterBag is much symfonish way.

2
  • 1
    I believe this is the default way to go from symfony 4.3+ ! Working in 5.2
    – Artandor
    Aug 6, 2021 at 11:29
  • 1
    With time you forget the basics. $_ENV should do the job for simple things like a functional test for example :) for more elaborated and Symfony way approach, use parameters.
    – davidmpaz
    Oct 13, 2022 at 15:30
15

Did you try with:

$this->getParameter('your parameter');

Edit:

This may help you -> https://symfony.com/doc/current/components/dotenv.html

4
  • 1
    Thank you for your answer :) I added the parameter foo in the .env file (foo=faa), I cleaared caches but I get an "InvalidArgumentException : The parameter 'foo' must be defined" in my controller :/ Sep 3, 2018 at 14:43
  • 1
    Edited my answer
    – AnTrakS
    Sep 3, 2018 at 14:45
  • 1
    Yes, I just saw that ! It's curious, I thinked that was a native method in Symfony ! Sep 3, 2018 at 14:46
  • I'm not very familiar with Symfony, there may be a native method in Symfony 4.
    – AnTrakS
    Sep 3, 2018 at 14:48
11

Before to user getParameter() you have to add this in services.yaml

parameters:
    your_parameter: '%env(your_parameter)%' # from .env file
8

Yes it is. You have 03 steps configuration :
Filstly - declare yours variables in env.
Secondly - configure service file
and finally - call your parameter in your controller :

_ In controllers extending from the AbstractController, use the getParameter() helper :

YAML file config

# config/services.yaml
parameters:
    kernel.project_dir: "%env(variable_name)%"
    app.admin_email: "%env(variable_name)%"

In your controller,

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class UserController extends AbstractController
{
    // ...

    public function index(): Response
    {
        $projectDir = $this->getParameter('kernel.project_dir');
        $adminEmail = $this->getParameter('app.admin_email');

        // ...
    }
}


_ If controllers not extending from AbstractController, inject the parameters as arguments of their constructors.

YAML file config

# config/services.yaml
parameters:
    app.contents_dir: "%env(variable_name)%"

services:
    App\Controllers\UserController :
        arguments:
            $contentsDir: '%app.contents_dir%'

In your controller,

class UserController 
{
    private $params;

    public function __construct(string $contentsDir)
    {
        $this->params = $contentsDir;
    }

    public function someMethod()
    {
        $parameterValue = $this->params;
        // ...
    }
}


_ Finally, if some controllers needs access to lots of parameters, instead of injecting each of them individually, you can inject all the application parameters at once by type-hinting any of its constructor arguments with the ContainerBagInterface:

YAML file config

# config/services.yaml
parameters:
    app.parameter_name: "%env(variable_name)%"

In your service,

use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;

class UserController 
{
    private $params;

    public function __construct(ContainerBagInterface $params)
    {
        $this->params = $params;
    }

    public function someMethod()
    {
        $parameterValue = $this->params->get('app.parameter_name');
        // ...
    }
}


source Accessing Configuration Parameters

1
  • apply corrections as suggested by @samuel-liew
    – belem
    Jan 24, 2022 at 13:15
6
  1. Add the variable in the config parameters :
    parameters:
        your_variable: '%env(YOUR_ENV_VARIABLE)%'
  1. Fetch it from the controller
    $var = $this->getParameter('your_variable');
2

In Symfony 4.4 this works fine:

<?php
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;

class SomeController extends AbstractController 
{

    public function someMethod(Request $request)
    {
        $parameterValue = $request->server->get('env_varname');
        // ...
    }
}

also in TWIG:

{{ app.request.server.get('env_varname') }}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.