如何从 Laravel 项目的根目录访问 env 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37925195/
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-09-14 14:02:04  来源:igfitidea点击:

How to access env file from the root of the Laravel Project?

phplaravellaravel-5

提问by cyber8200

I have a phpscript locate at the rootdirectory of my laravel project. I want to access my settings in my .env

我在Laravel 项目的目录中有一个php脚本。我想在我的 .env 中访问我的设置



I've tried

我试过了

$dbname      = env('DB_DATABASE');
$user        = env('DB_USERNAME');
$host        = env('DB_HOST');
$pass        = env('DB_PASSWORD');


I got

我有

PHP Fatal error:  Call to undefined function env()


What is the workaround for that ? Should I include anything to be able to access env()?

解决方法是什么?我应该包括任何能够访问的东西env()吗?

回答by jszobody

If you just want to load your .envconfig in a separate PHP script, you can do it like this:

如果您只想.env在单独的 PHP 脚本中加载您的配置,您可以这样做:

require_once "vendor/autoload.php";
Dotenv::load(__DIR__);

This will load your .envconfig into the $_ENVarray.

这会将您的.env配置加载到$_ENV数组中。

So now you can access your variables like this:

所以现在你可以像这样访问你的变量:

$dbname      = $_ENV['DB_DATABASE'];
$user        = $_ENV['DB_USERNAME'];
$host        = $_ENV['DB_HOST'];
$pass        = $_ENV['DB_PASSWORD'];

If you really want that env()helper function, just go get that file:

如果你真的想要那个env()辅助函数,就去获取那个文件:

require_once "vendor/laravel/framework/src/Illuminate/Foundation/helpers.php";

And now you can also access env('DB_DATABASE');

现在您还可以访问 env('DB_DATABASE');

Note: this does NOT give you a bootstrapped Laravel environment at all. It's a simple way to grab your .envconfig variables, but that's it.

注意:这根本不会给你一个引导的 Laravel 环境。这是获取.env配置变量的简单方法,仅此而已。

回答by thisisablock

Laravel is using vlucas/phpdotenvto read the .envfile. The env()function is Laravel helper / shortcut. To use the env command you have to require the vendor files and create an app / kernel laravel object

Laravel使用vlucas / phpdotenv读取.env文件。该env()功能是 Laravel 助手/快捷方式。要使用 env 命令,您必须需要供应商文件并创建一个应用程序/内核 laravel 对象

<?php

require __DIR__.'/vendor/autoload.php';

$app = require_once __DIR__.'/bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

\Dotenv::load($app->environmentPath(), $app->environmentFile());   
var_dump(env('DB_DATABASE'));

回答by d.raev

This is simple working solution as of 2018/ Laravel 5.6

这是2018/ Laravel 5.6 的简单工作解决方案

//assumes this script is in the root folder, same as .env
require_once "vendor/autoload.php";
(new \Dotenv\Dotenv(__DIR__))->load(); 

$count = $_ENV['UNICORN_COUNT'];

Troubleshooting:
- make sure Dotenv file is correctly called.
- check the Dotenv->load()(and Dotenv->__constructor()) and supply the needed params.

故障排除:
- 确保正确调用 Dotenv 文件。
- 检查Dotenv->load()(和Dotenv->__constructor())并提供所需的参数。

回答by Gerard

With newest version of DotEnv, use this to use the env()function

使用最新版本的 DotEnv,使用它来使用该env()功能

require_once "vendor/autoload.php";

$dotenv = Dotenv::create(__DIR__); // Dir where your .env file is located

$dotenv->load();

回答by Osura Dasith Gamalath

Had the same issue for me once fixed by below code

通过以下代码修复后对我有同样的问题

require_once dirname($_SERVER['DOCUMENT_ROOT'])."/vendor/autoload.php";//fetch the document path and removes the public folder using diranme function
$dotenv= Dotenv::create(dirname($_SERVER['DOCUMENT_ROOT']));// Dir where your .env file is located
$dotenv=$dotenv->load();
$url=$dotenv["APP_URL"];