32

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?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
spacecodeur
  • 2,206
  • 7
  • 35
  • 71

7 Answers7

55

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');
Mayank Tiwari
  • 919
  • 1
  • 6
  • 13
  • 1
    Works in 4.3 too. – learner May 07 '21 at 12:58
  • 2
    You can simply do `$_SERVER['APP_PARAM']` with value only in .env file https://symfony.com/doc/current/cloud/cookbooks/env.html#accessing-environment-variables – TikTaZ Sep 27 '21 at 14:04
  • To be able to call `$this->getParameter('app.paramname')` make sure your controller `extends AbstractController`. – iloo Dec 01 '21 at 14:27
  • The `config/services.yaml` part is only required if your controller DOES NOT `extends AbstractController`. https://symfony.com/doc/current/configuration.html#accessing-configuration-parameters – Jibato Jul 13 '22 at 09: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.

Theva
  • 873
  • 8
  • 15
  • 1
    I believe this is the default way to go from symfony 4.3+ ! Working in 5.2 – Artandor Aug 06 '21 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 '22 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

AnTrakS
  • 733
  • 4
  • 18
11

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

parameters:
    your_parameter: '%env(your_parameter)%' # from .env file
Aymeric
  • 347
  • 5
  • 17
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

belem
  • 341
  • 4
  • 3
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');
aneth101
  • 509
  • 5
  • 9
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') }}
unbreak
  • 1,000
  • 1
  • 16
  • 32