久久久久久久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 重定向到登錄頁面)
主站蜘蛛池模板: 中文精品久久 | 一区二区三区欧美 | 国产乱码精品一品二品 | 日本免费一区二区三区 | 亚洲综合二区 | 国产精品1区2区 | 日韩精品一区二区三区 | 亚洲高清在线视频 | 欧美三级在线 | 蜜桃一区二区三区 | 国产1区| 久久久久久免费毛片精品 | 国产女人精品视频 | jizz18国产 | 日韩美女在线看免费观看 | 91久久久久 | 久久精品综合网 | 一区二区不卡高清 | 欧美一级特黄aaa大片在线观看 | 一本色道久久综合亚洲精品高清 | 国产精品国产三级国产aⅴ中文 | 国产伦精品一区二区三区高清 | 成人福利 | 一区二区三区av | 欧美视频成人 | 欧美福利网站 | 久久尤物免费一区二区三区 | 欧美一级片中文字幕 | 伊人看片 | 91亚洲精品久久久电影 | 亚洲精品中文字幕在线 | 中文字幕91av | 亚洲精品99 | 欧美久久久久 | 国内精品久久精品 | 99久久中文字幕三级久久日本 | 午夜视频一区二区 | 色婷婷精品久久二区二区蜜臂av | 一区二区三区亚洲精品国 | 综合在线视频 | 中文字幕一区二区三区四区不卡 |