Laravel 动态配置设置

2021-12-18 00:00:00 php laravel laravel-5 laravel-5.2

我在我的项目中使用了一个包,它在 config/packagename

中存储了一个设置

我想在配置文件中动态更改这个值,这是当前文件结构的样子;

'118754561','cache_lifetime_in_minutes' =>60*24,];

我想把它改成这样 -

'view_id' =>身份验证::用户()-> id,

您可以在配置文件中执行此操作,还是必须存储某种变量以便稍后在控制器中更新.有没有办法将这些变量放在 env 文件中并从控制器访问这些新变量?

解决方案

这也是一个动态更新 .env 文件的通用解决方案(分别是单独的键/值对)

  1. 像这样更改 config/packagename 中的设置:

<块引用>

返回 ['view_id' =>环境('VIEW_ID','118754561'),等等...]

  1. 在 .env 中添加一个初始值:

    VIEW_ID=118754561

  2. 在适当的控制器(例如 AuthController)中,使用下面的代码并像这样调用函数:updateDotEnv('VIEW_ID', Auth::User()->id)

    受保护的函数 updateDotEnv($key, $newValue, $delim=''){$path = base_path('.env');//从当前环境中获取旧值$oldValue = env($key);//有什么变化吗?如果($oldValue === $newValue){返回;}//用改变的数据重写文件内容如果(文件存在($路径)){//用新值替换当前值file_put_contents($path, str_replace($key.'='.$delim.$oldValue.$delim,$key.'='.$delim.$newValue.$delim,file_get_contents($path)));}}

(如果您想让此函数更通用,以便使用 .env 中的 key=value 对,其中值必须用双引号括起来,因为它们包含空格,则需要 $delim 参数).

诚然,如果您有多个用户同时在您的项目中使用这个包,这可能不是一个好的解决方案.所以这取决于你使用这个包的目的.

注意:如果您打算从其他类中使用它,当然需要将该函数公开.

I'm using a package within my project and it stores a setting inside config/packagename

I would like to dynamically change this value inside the config file, this is how the file structure looks currently;

<?php

return [
    'view_id' => '118754561',

    'cache_lifetime_in_minutes' => 60 * 24,
];

I would like to change it to something like this -

'view_id' => Auth::user()->id,

Can you do this within the config file, or do you have to store some sort of variable to be updated later within a controller. Is there a way to place these variables in an env file and access these new variables from a controller?

解决方案

This also is a generic solution to dynamically update your .env file (respective the individual key/value pairs)

  1. Change the setting in your config/packagename like so:

return [
    'view_id' => env('VIEW_ID', '118754561'),
    etc...
]

  1. Add an initial value into .env:

    VIEW_ID=118754561

  2. In an appropriate controller (e.g. AuthController), use the code below and call the function like this: updateDotEnv('VIEW_ID', Auth::User()->id)

    protected function updateDotEnv($key, $newValue, $delim='')
    {
    
        $path = base_path('.env');
        // get old value from current env
        $oldValue = env($key);
    
        // was there any change?
        if ($oldValue === $newValue) {
            return;
        }
    
        // rewrite file content with changed data
        if (file_exists($path)) {
            // replace current value with new value 
            file_put_contents(
                $path, str_replace(
                    $key.'='.$delim.$oldValue.$delim, 
                    $key.'='.$delim.$newValue.$delim, 
                    file_get_contents($path)
                )
            );
        }
    }
    

(The $delim parameter is needed if you want to make this function more generic in order to work with key=value pairs in .env where the value has to be enclosed in double quotes because they contain spaces).

Admittedly, this might not be a good solution if you have multiple users at the same time using this package in your project. So it depends on what you are using this package for.

NB: You need to make the function public of course if you plan to use it from other classes.

相关文章