php 在刀片中访问 Laravel .env 变量

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

Accessing Laravel .env variables in blade

phplaravel

提问by Anna Jeanine

I am trying to get some API keys which I have stored in my .env file to use in the blade javascript. I have added two keys like:

我正在尝试获取一些存储在 .env 文件中的 API 密钥,以便在刀片 javascript 中使用。我添加了两个键,如:

APP_ENV=local
APP_KEY=////
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost
APP_GOOGLE_MAPS=////
APP_OVERHEID_IO=////

In blade I need to use the Google Maps API and OverheidIO API key. I have tried getting one of the default .env variables just in case I have formatted the custom .env variables wrong.:

在刀片中,我需要使用 Google Maps API 和 OverheidIO API 密钥。我曾尝试获取默认的 .env 变量之一,以防万一我将自定义 .env 变量格式化为错误。:

{{ env('APP.ENV') }} // nothing
{{ env('APP_ENV') }} // nothing
{{ env('APP_ENV'), 'test' }} // returns 'test' 

Could someone help me call the google maps api and overheidio api key in the blade?

有人可以帮我调用刀片中的谷歌地图 api 和 overheidio api 密钥吗?

回答by Learner

Five most important commands if your Laravel is not working as expected after some modifications in .env or database folder or because of any other modifications. Here is full explanation: https://www.youtube.com/watch?v=Q1ynDMC8UGg

如果您的 Laravel 在 .env 或数据库文件夹中进行了一些修改或由于任何其他修改后无法按预期工作,那么五个最重要的命令。这是完整的解释:https: //www.youtube.com/watch?v=Q1ynDMC8UGg

php artisan config:clear
php artisan cache:clear
composer dump-autoload
php artisan view:clear
php artisan route:clear

回答by Armando Cordova

I have it implemented in the following way:

我通过以下方式实现了它:

@if (env('APP_ENV')!='Production')
Enviroment Test
@endif

My recommendation is to execute the following command: composer self-update

我的建议是执行以下命令: composer self-update

回答by ndberg

VERY IMPORTANT

很重要

All env() like: env('APP_ENV')calls WON'T WORKin production (when you use php artisan config:cache)

所有 env() 之类的:在生产中env('APP_ENV')调用WON'T WORK(当您使用时php artisan config:cache

What to use?
- use env() only in config files
- use App::environment() for checking the environment (APP_ENV in .env).
- use config('app.var') for all other env variables, ex. config('app.debug')
- create own config files for your own ENV variables. Example:
In your .env:

用什么?
- 仅在配置文件中使用 env()
- 使用 App::environment() 检查环境(.env 中的 APP_ENV)。
- 将 config('app.var') 用于所有其他环境变量,例如。config('app.debug')
- 为您自己的 ENV 变量创建自己的配置文件。示例:
在您的 .env 中:

MY_VALUE=foo

example config app/myconfig.php

示例配置应用程序/myconfig.php

return [
    'myvalue' => env('MY_VALUE', 'bar'), // 'bar' is default if MY_VALUE is missing in .env
];

Access in your code:

在您的代码中访问:

config('myconfig.myvalue') // will result in 'foo'


More details see HERE

更多详情请看这里

回答by Jnanaranjan

If you want to get the environment of the app then try this:

如果您想获取应用程序的环境,请尝试以下操作:

{{App::environment()}}

I have not tried other variables.

我没有尝试过其他变量。

回答by Alex

It causes problems to use env() anywhere else than in the config/ folder. Use env in there and then config () in the other parts of the app

在 config/ 文件夹以外的任何地方使用 env() 都会导致问题。在那里使用 env 然后在应用程序的其他部分使用 config()

回答by Luca C.

php artisan config:clear

should fix it

应该修复它

回答by Mhar Daniel

get values here: config/app.php


in blade:

在此处获取值:config/app.php


在刀片中:

{{ config('app.name', 'default value here') }}

in class/controller:

在类/控制器中:

config('app.name', 'default value here')

回答by JR Lawhorne

Here's a link to the documentation: https://laravel.com/docs/6.x/configuration#retrieving-environment-configuration

这是文档的链接:https: //laravel.com/docs/6.x/configuration#retrieving-environment-configuration

In the sample below, I spit out the actual error when I'm in my development environment but give a generic message if in any other environment.

在下面的示例中,当我在我的开发环境中时,我会吐出实际的错误,但如果在任何其他环境中,则会给出一个通用消息。

@if(App::environment('development'))
    Error: {{ $record->s_error }}
@else
    XML Parsing Error - Please double check that your file is formatted correctly.
@endif

回答by medard mandane

You should only access .envvalues directly inside configuration files, then access them from everywhere (controllers, views) from configuration files using config()helper

For example:

您应该只.env直接访问配置文件中的值,然后使用config()helper 从配置文件中的任何地方(控制器、视图)访问它们

例如:

.env

TEST_URL=http://test

.env

TEST_URL=http://test


config/app.php

return [
   'test_url' => env('TEST_URL','http://default.url')
];

配置/app.php

return [
   'test_url' => env('TEST_URL','http://default.url')
];


resources/views/welcome.blade.php

{{ config('app.test_url')}}

see configuration cachingfrom laravel documentation for more info.

资源/视图/welcome.blade.php

{{ config('app.test_url')}}

有关更多信息,请参阅laravel 文档中的配置缓存

回答by ABHISHEK SHUKLA

i was also having trouble getting value from .env file, then i did this and it helped :

我也无法从 .env 文件中获取价值,然后我这样做了,它有帮助:

  1. Check env file and see if you have given the correct value.
  2. then check blade or controller where you using that variable from .env file.
  3. if both steps above goes right, you just need to do these steps -
  1. 检查 env 文件,看看您是否给出了正确的值。
  2. 然后从 .env 文件中检查您使用该变量的刀片或控制器。
  3. 如果上述两个步骤都正确,您只需要执行以下步骤 -

php artisan config:clear
php artisan cache:clear
php artisan view:clear
php artisan route:clear
composer dump-autoload

php artisan config:clear
php artisan cache:clear
php artisan view:clear
php artisan route:clear
composer dump-autoload