无法从Symfony服务访问dotenv变量

时间:2019-06-20 19:39:29

标签: php symfony dotenv

使用:

var webView: WKWebView!

我无法从Symfony服务访问.env和.env.local文件中的值。我收到“通知:未定义的索引”错误。

我看到应用程序正在从config / bootstrap.php加载.env和.env.local

        "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.*"

我还检查了以下文件,看是否与dotenv有关,但没有发现

  • public / index.php
  • web / app_dev.php
  • web / app.php
  • config / packages / framework.yaml

这是我尝试访问变量的方法:

(new Dotenv(false))->loadEnv(dirname(__DIR__).'/.env', dirname(__DIR__).'/.env.local');

这是我在日志中看到的:

<?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);
    }

}

更新(根据下面的Ugo评论)

如果我将以下内容添加到config / services.yaml

request.CRITICAL: Uncaught PHP Exception ErrorException: "Notice: Undefined index: CORS_ALLOW_ORIGIN"...

并更新我的服务:

    App\Manager\Studio\StudioManager:
        arguments:
            - '%env(GOOGLE_GEOCODE_URL)%'
            - '%env(GOOGLE_GEOCODE_KEY)%'

我收到以下错误:

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;

    }

因此,如果我按照错误提示进行操作:

Cannot autowire service App\Manager\Studio\StudioManager: argument $google_geocode_url of method __construct() is type-hinted stringyou should configure its value explicitly

我遇到了另一个错误:

    App\Manager\Studio\StudioManager:
        arguments:
            $google_geocode_url: '%env(GOOGLE_GEOCODE_URL)%'
            $google_geocode_key: '%env(GOOGLE_GEOCODE_KEY)%'

另外,如果我运行php bin / console,我确实会看到Environment(.env)部分下列出的变量。

好像我在转圈

1 个答案:

答案 0 :(得分:0)

您可以像这样在服务声明中传递此环境变量:

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

    }
    ....

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

// Studio class

public function setYourDotEnvVar(string $dotEnvVar)
{
    $this->dotEnvVar = $dotEnvVar;  
}