Laravel 动态配置设置

Laravel dynamic config settings(Laravel 动态配置设置)
本文介绍了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 动态配置设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)