Laravel-配置界面操作处理

以邮箱配置为例

image.png

1. config辅助函数
提取数据
config("mail.defaut")
设置数据
config(["mail.defaut"=>"ddddd"])
2. 数据库存储 配置信息存储在数据中 自定义实现即可

模型

    

控制器

    config("mail.default"),
                "MAIL_HOST"=>config("mail.mailers.smtp.host"),
                "MAIL_PORT"=>config("mail.mailers.smtp.port"),
                "MAIL_USERNAME"=>config("mail.mailers.smtp.username"),
                "MAIL_PASSWORD"=>config("mail.mailers.smtp.password"),
                "MAIL_ENCRYPTION"=>config("mail.mailers.smtp.encryption"),
                "MAIL_FROM_ADDRESS"=>config("mail.from.address"),
                "MAIL_FROM_NAME"=>config("mail.from.name")
            ];
            return response(["data"=>$emailSettings]);
        }
        public function update(Request $request){
            $check = validator($request->all(),[ 
                'MAIL_MAILER'  =>'required',
                'MAIL_HOST'  =>'required',
                'MAIL_PORT'  =>'required',
                'MAIL_USERNAME'  =>'required',
                'MAIL_PASSWORD'  =>'required',
                'MAIL_ENCRYPTION'  =>'required',
                'MAIL_FROM_ADDRESS'  =>'required',
                'MAIL_FROM_NAME'  =>'required',
            ]);
            if($check->passes()){ 
                Setting::whereIn("setting_key",$this->keys)->delete();
                $settings = [];
                foreach($this->keys as $key){
                    $settings[$key] = [
                        "setting_key"=>$key,
                        "setting_type"=>"string",
                        "setting_value"=>$request->input($key)
                    ];
                }
                Setting::insert($settings);
                return response(['message'=>"数据已保存!"]);
            }else{ 
                return response(['check_errors'=>$check->errors()],400);
            }
        }
        public function test(Request $request){

        }
    }
3. 中间件 通过中间件修改对应路由下的邮箱配置
    "mail.default",
                "MAIL_HOST"=>"mail.mailers.smtp.host",
                "MAIL_PORT"=>"mail.mailers.smtp.port",
                "MAIL_USERNAME"=>"mail.mailers.smtp.username",
                "MAIL_PASSWORD"=>"mail.mailers.smtp.password",
                "MAIL_ENCRYPTION"=>"mail.mailers.smtp.encryption",
                "MAIL_FROM_ADDRESS"=>"mail.from.address",
                "MAIL_FROM_NAME"=>"mail.from.name",
            ];
            $settings = Setting::whereIn("setting_key",collect($keys)->keys()->toArray())->get();
            foreach ($settings as $item) {
                config([$keys[$item->setting_key]=>$item->setting_value]);
            }
            return $next($request);
        }
    }

你可能感兴趣的:(laravel)