Laravel 动态配置设置

2022-10-18php开发问题
88

本文介绍了Laravel 动态配置设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在我的项目中使用了一个包,它在 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.

这篇关于Laravel 动态配置设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End
Laravel

相关推荐

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

如何解决“无法与主机 smtp.gmail.com 建立连接"?
How can I solve quot;Connection could not be established with host smtp.gmail.comquot;?(如何解决“无法与主机 smtp.gmail.com 建立连接?)...
2024-08-23 php开发问题
11

带有队列 550 错误的 Laravel 电子邮件(每秒电子邮件太多)
Laravel email with queue 550 error (too many emails per second)(带有队列 550 错误的 Laravel 电子邮件(每秒电子邮件太多))...
2024-08-23 php开发问题
9

Laravel 如何处理 PHP 警告?
How Laravel handles PHP warnings?(Laravel 如何处理 PHP 警告?)...
2024-08-23 php开发问题
12

如何从 laravel 应用程序访问全局变量 $_SESSION 和 $_COOKIE?
How to access the globals $_SESSION and $_COOKIE from a laravel app?(如何从 laravel 应用程序访问全局变量 $_SESSION 和 $_COOKIE?)...
2024-08-14 php开发问题
0

如何在 laravel 5.3 中对多张图片上传进行验证
How to do validation for multiple images upload in laravel 5.3(如何在 laravel 5.3 中对多张图片上传进行验证)...
2024-08-14 php开发问题
0