close

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

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Symfony 4, get .env parameter from a controller, is it possible and how? in php. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

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

Today I get the following error Symfony 4, get .env parameter from a controller, is it possible and how? in php.

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

  1. How To Solve Symfony 4, get .env parameter from a controller, is it possible and how Error ?

    To Solve Symfony 4, get .env parameter from a controller, is it possible and how Error Before to user getParameter() you have to add this in services.yaml

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

    To Solve Symfony 4, get .env parameter from a controller, is it possible and how Error Before to user getParameter() you have to add this in services.yaml

Solution 1

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.

Solution 2

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

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

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read

Leave a Comment