0

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

miken32
  • 42,008
  • 16
  • 111
  • 154

1 Answers1

0

You can pass this env var in your service declaration like that :

App\Manager\Studio:
    arguments:
        ... other args
        - '%env(CORS_ALLOW_ORIGIN)%'

// Studio class
....

public function __construct(
        EntityManagerInterface $em,
        LoggerInterface $logger,
        string $dotEnvVar
    ) {
        parent::__construct($logger, $translator);

        $this->em              = $em;
        $this->logger          = $logger;
        $this->dotEnvVar       = $dotEnvVar;

    }

    public function createOneFromRequest(CustomRequest $request)
    {
        $this->logger->critical($this->dotEnvVar);

    }
    ....

or

App\Manager\Studio:
        calls:
        - method: setYourDotEnvVar
          arguments:
              - '%env(CORS_ALLOW_ORIGIN)%'

// Studio class

public function setYourDotEnvVar(string $dotEnvVar)
{
    $this->dotEnvVar = $dotEnvVar;  
}
Ugo T.
  • 1,068
  • 8
  • 12
  • Unfortunately that did not work. I added your suggestion to my config/services,yaml and corrected it to App\Manager\Studio\StudioManager (which is the full namespace path), but I get the same error –  Jun 21 '19 at 13:05
  • Did you update your service constructor and retrieved the new arg from it ? (I have update my answer to make it more detailed) – Ugo T. Jun 21 '19 at 13:27
  • I am still running into troubles, I have update my question to show what is happening! –  Jun 21 '19 at 14:30
  • The arguments listed in the yml must match the arguments of your service constructor. Alternatively, you can [call a setter directly from your yml](https://symfony.com/doc/current/service_container/calls.html). – Ugo T. Jun 21 '19 at 14:56
  • That produces the same error that was raised before: Cannot autowire service App\Manager\Studio\StudioManager: argument $google_geocode_url of method __construct() is type-hinted stringyou should configure its value explicitly –  Jun 21 '19 at 15:54
  • All right, have you tried to dump() `$_ENV`, `$_SERVER` or to use `getenv('GOOGLE_GEOCODE_URL')` to see if this vars are somewhere ? – Ugo T. Jun 21 '19 at 16:41
  • $_ENV has only {"SHELL_VERBOSITY":3} and $_SERVER has a bunch of entries, none of which are from the .env file, they are all from the nginx conf and site conf. –  Jun 21 '19 at 17:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/195399/discussion-between-nate-and-ugo-t). –  Jun 22 '19 at 13:03