如何访问 javascript 中的 laravel .env 变量?

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

How to access the laravel .env variables inside javascript?

javascriptphplaravelenvironment-variables

提问by Stephan-v

I am currently using Algolia for my search engine inside a Laravel application, so whenever a record is created it is also sent to the Agolia database. Of course I want to separate my data in the Algolia database with a testset for local development and a production set for the live website. I have defined which indices of my Algolia database are being used in a JavaScript file.

我目前在 Laravel 应用程序中使用 Algolia 作为我的搜索引擎,因此每当创建记录时,它也会发送到 Agolia 数据库。当然,我想将 Algolia 数据库中的数据与本地开发的测试集和实时网站的生产集分开。我已经定义了在 JavaScript 文件中使用了我的 Algolia 数据库的哪些索引。

Now I am wondering how do I react accordingly to my APP_ENV variable to change it depending on my current environment? Obviously I need to put stuff into an ifstatement in my JavaScript but how do I make my javascript access the .env variables properly?

现在我想知道如何根据我当前的环境对我的 APP_ENV 变量做出相应的反应来改变它?显然我需要if在我的 JavaScript 中放入一个语句,但是如何让我的 javascript 正确访问 .env 变量?

Hopefully somebody can help me out.

希望有人可以帮助我。

Cheers.

干杯。

回答by Agu Dondo

You can just echo out the env variable as a javascript string in your view:

您可以在视图中将 env 变量作为 javascript 字符串回显:

<script>

var name = '{{ env('NAME') }}';

alert(name);

</script>

And your .env file would look like this:

你的 .env 文件看起来像这样:

NAME=Alex

姓名=亚历克斯

回答by TimothePearce

As the documentation suggest it: you can now easily use envvariables by prefixing a key in your .env file with MIX_.

正如文档所暗示的那样:您现在可以env通过在 .env 文件中为键添加前缀来轻松使用变量MIX_

MIX_SENTRY_DSN_PUBLIC=http://example.com

MIX_SENTRY_DSN_PUBLIC=http://example.com

After the variable has been defined in your .envfile, you may access via the process.envobject.

.env文件中定义变量后,您可以通过process.env对象访问。

process.env.MIX_SENTRY_DSN_PUBLIC

process.env.MIX_SENTRY_DSN_PUBLIC

回答by simonw16

If accessing the env vars from outside of the main js file. You can do the following:

如果从主 js 文件之外访问环境变量。您可以执行以下操作:

let mix = require('laravel-mix');
require('dotenv').config();

then:

然后:

let my_env_var = process.env.MY_ENV_VAR;

Requiring mix and the dotenv config gives you access. :)

需要 mix 和 dotenv 配置才能访问。:)

回答by Alexey Mezenin

There are a lot of ways.

有很多方法。

One of the best is to pass data from Controller to the view:

最好的方法之一是将数据从 Controller 传递到视图:

if ($app->environment('local'))
{
    // Here you can generate some <script></script> code or you could just generate
    // hidden div like <div id="environment" style="display:none">local</div>

}

or like this:

或者像这样:

return view('myView')->with('env', $app->environment());

Or you could use Sessions:

或者你可以使用会话:

\Session::put('env', $app->environment())

Of course you can create dedicated route and get it with Ajax, but I don't think it's a good way to do this in real life web application.

当然,您可以创建专用路由并使用 Ajax 获取它,但我认为这不是在现实生活中的 Web 应用程序中执行此操作的好方法。

回答by TimothePearce

A cleaner solution is to use the laracasts/utilitiespackage to send your variables to your file.

更简洁的解决方案是使用laracasts/utilities包将变量发送到文件。

Install it with the composer require laracasts/utilitiescommand.

使用composer require laracasts/utilities命令安装它。

Then add use JavaScript;at the top of your controller.

然后use JavaScript;在控制器的顶部添加。

And finally send your variable this way : JavaScript::put([ 'foo' => $bar ]);.

最后发送您的变量是这样的: JavaScript::put([ 'foo' => $bar ]);