简体   繁体   English

如何从laravel中的配置文件访问变量

[英]how to access variable from config file in laravel

I have one file inside config folder lets say: file1我在 config 文件夹中有一个文件可以说: file1

File Location:
    config/
    ---file1.php

I have following code in my file: file1.php我的文件中有以下代码: file1.php

return [
 'MASTER_KEY' => ['SUB_KEY1' => 'SOME_VALUE', 'SUB_KEY2' => 'SOME_VALUE', 'SUB_KEY3' => 'SOME_VALUE'],
];

How to access the value from MASTER_KEY of particular SUB_KEY ?如何访问从价值MASTER_KEY特定的SUB_KEY

Assuming for SUB_KEY2 , Try -假设SUB_KEY2 ,尝试 -

config('file1.MASTER_KEY.SUB_KEY2')

Guide 指导

In order to access that value you have 2 choices which are the same at the end:为了访问该值,您在最后有 2 个相同的选择:

first one: which use Config refers to the config facade.第一个:其中use Config是指配置门面。

use Config;

$myValue = Config::get('file1.MASTER_KEY.SUB_KEY1');
enter code here

second one: using config() helper function which uses Config facade and its only an alternative and easy way to use Config facade.第二个:使用config()它使用辅助功能Config的立面和其仅使用一个替代的和容易的方式Config外观。

$myValue = config('file1.MASTER_KEY.SUB_KEY1');

Make a use Config;做一个使用Config; in your controller.在您的控制器中。 In config/file1.php在 config/file1.php

<?php return [
    'ADMIN_NAME' => 'administrator',
    'EMP_IMG_PATH' => '../uploads/profile/original',
    'EMP_DOC_PATH' => '../uploads/profile/documents', ];

In controller在控制器中

use Config;
$variable= Config::get('file1.EMP_DOC_PATH');

By this way you can get the variables value from config.通过这种方式,您可以从配置中获取变量值。 I think this will make something good.我认为这会有所作为。

use

config('file1.keyname');

if you have made any changes in config file then it might not work .如果您对配置文件进行了任何更改,则它可能无法正常工作。 so after changes in config file you have to run the following two commands .所以在配置文件更改后,您必须运行以下两个命令。

php artisan config:cache

php artisan config:clear

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM