Using:
"php": "^7.1.3",
"ext-ctype": "*",
"ext-iconv": "*",
"doctrine/doctrine-bundle": "^1.11",
"doctrine/doctrine-fixtures-bundle": "^3.1",
"doctrine/doctrine-migrations-bundle": "^2.0",
"doctrine/orm": "^2.6",
"friendsofsymfony/oauth-server-bundle": "^1.6",
"friendsofsymfony/rest-bundle": "^2.5",
"friendsofsymfony/user-bundle": "^2.1",
"jms/serializer-bundle": "^3.3",
"nelmio/api-doc-bundle": "^3.4",
"nelmio/cors-bundle": "^1.5",
"symfony/console": "4.2.*",
"symfony/debug": "4.2.*",
"symfony/dotenv": "4.2.*",
"symfony/event-dispatcher": "4.2.*",
"symfony/flex": "^1.1",
"symfony/framework-bundle": "4.2.*",
"symfony/http-foundation": "4.2.*",
"symfony/http-kernel": "4.2.*",
"symfony/maker-bundle": "^1.11",
"symfony/monolog-bundle": "^3.3",
"symfony/orm-pack": "^1.0",
"symfony/routing": "4.2.*",
"symfony/swiftmailer-bundle": "^3.2",
"symfony/translation": "4.2.*",
"symfony/yaml": "4.2.*"
I am not able to access values from my .env and .env.local files from a Symfony service. I get an "Notice: Undefined index" error.
I see the application is loading the .env and .env.local from the config/bootstrap.php
(new Dotenv(false))->loadEnv(dirname(__DIR__).'/.env', dirname(__DIR__).'/.env.local');
I have also checked the following files to see if there is anything to do with dotenv, and found nothing
- public/index.php
- web/app_dev.php
- web/app.php
- config/packages/framework.yaml
Here is how I try to access the variable:
<?php
namespace App\Manager\Studio;
use ...
class StudioManager
{
protected $em;
private $logger;
public function __construct(
EntityManagerInterface $em,
LoggerInterface $logger
) {
parent::__construct($logger, $translator);
$this->em = $em;
$this->logger = $logger;
}
public function createOneFromRequest(CustomRequest $request)
{
$this->logger->critical($_ENV['CORS_ALLOW_ORIGIN']);
}
public function getAllFromRequest()
{
return $this->getEntities(Studio::class);
}
}
And here is what I see in my log:
request.CRITICAL: Uncaught PHP Exception ErrorException: "Notice: Undefined index: CORS_ALLOW_ORIGIN"...
Update (as per Ugo comments below):
If I add the following to my config/services.yaml
App\Manager\Studio\StudioManager:
arguments:
- '%env(GOOGLE_GEOCODE_URL)%'
- '%env(GOOGLE_GEOCODE_KEY)%'
and update my service:
class StudioManager extends AbstractRestManager
{
protected $em;
private $logger;
private $translator;
private $google_geocode_url;
private $google_geocode_key;
public function __construct(
EntityManagerInterface $em,
LoggerInterface $logger,
TranslatorInterface $translator,
string $google_geocode_url,
string $google_geocode_key
) {
parent::__construct($logger, $translator);
$this->em = $em;
$this->logger = $logger;
$this->translator = $translator;
$this->google_geocode_url = $google_geocode_url;
$this->google_geocode_key = $google_geocode_key;
}
I get the following error:
Cannot autowire service App\Manager\Studio\StudioManager: argument $google_geocode_url of method __construct() is type-hinted stringyou should configure its value explicitly
So if I do as the error suggests:
App\Manager\Studio\StudioManager:
arguments:
$google_geocode_url: '%env(GOOGLE_GEOCODE_URL)%'
$google_geocode_key: '%env(GOOGLE_GEOCODE_KEY)%'
I get a different error:
"message": "Environment variable not found: \"GOOGLE_GEOCODE_URL\"
Also if I run php bin/console about, I do see the variables listed under the Environment(.env) section.
Seems I am going around in circles