【问题标题】:How to add and update config varaibles in laravel如何在 laravel 中添加和更新配置变量
【发布时间】:2017-05-10 05:31:01
【问题描述】:

我有一个配置变量文件,我在控制器中使用它而不是 mysql 以获得更快的性能。

但我的问题是我只能从这个配置文件中读取,我不能从中添加或更新任何值。

任何建议如何更新或添加新值到这个变量文件:

我的变量文件存储在/config/Banners_size.php:

return [
    "normal_x970h90" => [
        'status' => 'enable',
        'value' => '500'
    ],

    "normal_x234h60" => [
        'status' => 'enable',
        'value' => '500'
    ],
]

我的 php 代码向其中添加新数组,但它不起作用:

    $banners = Config('Banners_size');
    $banner = array(
        $request->input('size') => [
            'status' => $request->input('status'),
             'value' => $request->input('cost')
        ]
    );

   $bannerinfo = array_merge($banners, $banner);

   Config('Banners_size' , $bannerinfo);

【问题讨论】:

    标签: php arrays variables config laravel-5.3


    【解决方案1】:

    配置文件是只读文件,不能以编程方式向其中添加参数。

    $banners = config('Banners_size');
    
    config('Banners_size' , $bannerinfo);
    

    以上两行,作用相同,都返回Banner_size的值。

    【讨论】:

    • 所以没有办法写入这个文件?
    • 您只能通过将实际值写入文件来做到这一点。换句话说,在数组中追加新值,而不是在运行时。
    【解决方案2】:

    要在运行时设置配置值,请将数组传递给配置助手:

    config(['app.timezone' => 'America/Chicago']);
    

    注意 - 它不会为下一个请求保留数据

    Docs

    【讨论】:

      【解决方案3】:

      技术上你可以...

      //edit config for current runtime
      config(['FILE.KEY' => 'NEW_VALUE']);
      // open config file for writing
      $fp = fopen(base_path() .'/config/FILE.php' , 'w');
      // write updated runtime config to file
      fwrite($fp, '<?php return ' . var_export(config('FILE'), true) . ';');
      // close the file
      fclose($fp);
      // clear config cache
      Artisan::call('cache:clear');
      

      【讨论】:

        【解决方案4】:

        你可以简单地使用这个功能:

        // you have to stick to using single quotation (')
        public static function setConfig($config,$key,$value,$path)
        {
            file_put_contents(
                $path,
                str_replace("'$key'". ' => ' ."'" . config($config)."'",
                "'$key'". ' => ' ."'" . $value ."'",
                file_get_contents($path))
            );
            // clear config cache
            Artisan::call('cache:clear');
        }
        
        
        // Or use these. It makes no difference if you use a single or double quotation
        public static function setConfig($config,$key,$value,$path)
        {
            file_put_contents(
                $path,
                str_replace(["'$key'". ' => ' ."'" . config($config)."'","\"$key\"". ' => ' ."\"" . config($config)."\""],
                "'$key'". ' => ' ."'" . $value ."'",
                file_get_contents($path))
            );
            // clear config cache
            Artisan::call('cache:clear');
        }
        

        【讨论】:

          猜你喜欢
          • 2022-08-02
          • 2013-06-09
          • 2016-06-26
          • 2020-04-07
          • 1970-01-01
          • 2011-06-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多