Symfony 4, get .env parameter from a controller, is it possible and how?

Viewed 53030

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

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');

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.

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

parameters:
    your_parameter: '%env(your_parameter)%' # from .env file
  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');

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

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') }}
Related