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

Did you try with:

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

Edit:

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


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.


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

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

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