久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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 動態配置設置的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

add new element in laravel collection object(在 Laravel 集合對象中添加新元素)
Creating an edit modal in Laravel 5(在 Laravel 5 中創建編輯模式)
Laravel 5.5 API resources for collections (standalone data)(用于集合的 Laravel 5.5 API 資源(獨立數據))
What is the best practice to create a custom helper function in php Laravel 5?(在 php Laravel 5 中創建自定義輔助函數的最佳實踐是什么?)
No #39;Access-Control-Allow-Origin#39; header - Laravel(沒有“Access-Control-Allow-Origin標頭 - Laravel)
Laravel Passport Route redirects to login page(Laravel Passport Route 重定向到登錄頁面)
主站蜘蛛池模板: 亚洲国产18 | 日韩毛片在线观看 | 久久国产视频网站 | 玩丰满女领导对白露脸hd | av在线二区 | 成人国产精品久久久 | 欧美日韩一区在线 | 国产精品欧美一区喷水 | aaa在线| 欧美精品在线一区 | av中文字幕在线 | 91精品中文字幕一区二区三区 | 91操操操 | 日韩欧美视频 | 99re| 亚洲视频精品 | 亚洲精品视频免费观看 | 日韩成人在线观看 | 成人夜晚看av | 久久av一区二区三区 | 国产精品爱久久久久久久 | 国产精品久久久久久福利一牛影视 | 国产激情网 | 欧美久久精品一级黑人c片 91免费在线视频 | 毛片a级 | 日韩国产中文字幕 | 久久久久久久综合色一本 | 欧美三区视频 | 日本粉嫩一区二区三区视频 | 日本黄视频在线观看 | 日日操天天射 | 欧美成人精品 | 在线观看日韩av | 欧美激情精品久久久久久 | 午夜视频在线观看一区二区 | 欧美日韩免费视频 | 亚洲国产精品一区二区第一页 | 久久久久免费精品国产小说色大师 | 激情六月丁香婷婷 | 日韩欧美精品在线播放 | 日产久久|