php 在控制器中获取环境值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34263107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:51:39  来源:igfitidea点击:

Get environment value in controller

phplaravellaravel-5config

提问by nielsv

In my .env file I have the following:

在我的 .env 文件中,我有以下内容:

IMAP_HOSTNAME_TEST=imap.gmail.com
[email protected]
IMAP_PASSWORD_TEST=mypw

Now I would like to use them in my controller. I've tried this, but without any result:

现在我想在我的控制器中使用它们。我试过这个,但没有任何结果:

$hostname = config('IMAP_HOSTNAME_TEST');

The $hostname variable is equal to null. How can I use these configuration variables in my controller?

$hostname 变量等于 null。如何在我的控制器中使用这些配置变量?

回答by Chetan Ameta

Try it with:

试试看:

<?php $hostname = env("IMAP_HOSTNAME_TEST", "somedefaultvalue"); ?>

回答by Masum Ahmed Sarkar

It Doesn't work in Laravel 5.3+ if you want to try to access the value from the controller like below. It always returns null

如果您想尝试从控制器访问值,它在 Laravel 5.3+ 中不起作用,如下所示。它总是返回 null

<?php

    $value = env('MY_VALUE', 'default_value');

SOLUTION: Rather, you need to create a file in the configuration folder, say values.php and then write the code like below

解决方案:相反,您需要在配置文件夹中创建一个文件,比如 values.php,然后编写如下代码

File values.php

文件values.php

<?php

    return [

        'myvalue' => env('MY_VALUE',null),

        // Add other values as you wish

Then access the value in your controller with the following code

然后使用以下代码访问控制器中的值

<?php

    $value = \Config::get('values.myvalue')

Where "values" is the filename followed by the key "myvalue".

其中“values”是文件名,后跟“myvalue”键。

回答by Grant

To simplify: Only configuration files can access environment variables- and then pass them on.

简化:只有配置文件可以访问环境变量- 然后传递它们。

Step 1.)Add your variable to your .envfile, for example,

步骤 1.)将您的变量添加到您的.env文件中,例如,

EXAMPLE_URL="http://google.com"

Step 2.)Create a new file inside of the configfolder, with any name, for example,

步骤 2.)config文件夹内创建一个新文件,使用任何名称,例如,

config/example.php

Step 3.)Inside of this new file, I add an array being returned, containing that environment variable.

第 3 步。)在这个新文件中,我添加了一个返回的数组,其中包含该环境变量。

<?php
return [
  'url' => env('EXAMPLE_URL')
];

Step 4.)Because I named it "example", my configuration 'namespace' is now example. So now, in my controller I can access this variable with:

第 4 步。)因为我将它命名为“example”,所以我的配置“namespace”现在是 example。所以现在,在我的控制器中,我可以通过以下方式访问这个变量:

$url = \config('example.url');

Tip- if you add use Config;at the top of your controller, you don't need the backslash (which designates the root namespace). For example,

提示- 如果use Config;在控制器顶部添加,则不需要反斜杠(指定根命名空间)。例如,

namespace App\Http\Controllers;
use Config; // Added this line

class ExampleController extends Controller
{
    public function url() {
        return config('example.url');
    }
}

--- IMPORTANT ---Remember to enter php artisan config:cacheinto the console once you have created your example.php file. Configuration files and variables are cached, so if you make changes you need to flush that cache - the same applies to the .envfile being changed / added to.

--- 重要 ---php artisan config:cache创建 example.php 文件后,请记住进入控制台。配置文件和变量被缓存,因此如果您进行更改,您需要刷新该缓存 - 这同样适用于.env正在更改/添加到的文件。

回答by Clean code

All of the variables listed in .envfile will be loaded into the $_ENVPHP super-global when your application receives a request. Check out the Laravel configuration page.

当您的应用程序收到请求时,.env文件中列出的所有变量都将加载到$_ENVPHP 超级全局变量中 。查看 Laravel 配置页面

$_ENV['yourkeyhere'];

回答by Hamid Parchami

It's a better idea to put your configuration variables in a configuration file.

将配置变量放在配置文件中是一个更好的主意。

In your case, I would suggest putting your variables in config/mail.phplike:

在您的情况下,我建议将您的变量放在config/mail.php 中,例如:

'imap_hostname' => env('IMAP_HOSTNAME_TEST', 'imap.gmail.com')

And refer to them by

并通过

config('mail.imap_hostname')

It first tries to get the configuration variable value in the .envfile and if it couldn't find the variable value in the .envfile, it will get the variable value from file config/mail.php.

它首先试图获得在配置变量值.ENV文件,如果它不能找到在变量值.ENV文件,它将会从文件中的变量值配置/ mail.php

回答by Zarkys Salas

You can use with this format (tested on Laravel 5.5). In my case I used it for gettting the data of database connections and use on Controller:

您可以使用这种格式(在 Laravel 5.5 上测试)。在我的例子中,我用它来获取数据库连接的数据并在控制器上使用:

$User = env('DB_USERNAMEchild','');
$Pass = env('DB_PASSWORDchild','');

The second parameter can be null, or set any default in case of DB_USERNAMEchild is null.

第二个参数可以为空,或者在 DB_USERNAMEchild 为空的情况下设置任何默认值。

Your .env file can be the same:

您的 .env 文件可以相同:

DB_HOST=localhost
DB_DATABASE=FATHERBD
DB_USERNAME=root
DB_PASSWORD=password

DB_DATABASEchild=ZTEST
DB_USERNAMEchild=root
DB_PASSWORDchild=passwordofchild

回答by Anandan K

In Controller

在控制器中

$hostname = $_ENV['IMAP_HOSTNAME_TEST']; (or) $hostname = env('IMAP_HOSTNAME_TEST');

In blade.view

在blade.view中

{{$_ENV['IMAP_HOSTNAME_TEST']}}

回答by Rajib

You can not access the environment variable like this.

您不能像这样访问环境变量。

Inside the .envfile you write

.env你写的文件里面

IMAP_HOSTNAME_TEST=imap.gmail.com  // I am okay with this

Next, inside the configfolder there is a file, mail.php. You may use this file to code. As you are working with mail functionality. You might use another file as well.

接下来,config文件夹内有一个文件,mail.php。您可以使用此文件进行编码。当您使用邮件功能时。您也可以使用另一个文件。

return [

//..... other declarations
'imap_hostname_test' => env('IMAP_HOSTNAME_TEST'),
// You are hiding the value inside the configuration as well

];

You can call the variable from a controller using 'config(. Whatever file you are using inside config folder. You need to use that file name (without extension) + '.' + 'variable name' + ')'. In the current case you can call the variable as follows.

您可以使用'config(. 无论您在 config 文件夹中使用什么文件。你需要使用那个file name (without extension) + '.' + 'variable name' + ')'。在当前情况下,您可以按如下方式调用变量。

$hostname = config('mail.imap_hostname_test');

Since you declare the variable inside mail.php and the variable name is imap_hostname_test, you need to call it like this. If you declare this variable inside app.phpthen you should call

因为你在mail.php中声明了变量,变量名是imap_hostname_test,你需要这样调用它。如果你在里面声明这个变量,app.php那么你应该调用

$hostname = config('app.imap_hostname_test');

回答by T.J.

Maybe it is not related, but it might help someone... in Laravel just use dd(config('app.env'));and you'll see 'local' or 'production'.

也许它不相关,但它可能会帮助某人......在 Laravel 中只需使用dd(config('app.env'));,您就会看到“本地”或“生产”。