关于laravel:Laravel配置界面操作处理

56次阅读

共计 2668 个字符,预计需要花费 7 分钟才能阅读完成。

以邮箱配置为例

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

模型

    <?php

    namespace App\Models;

    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;

    class Setting extends Model
    {
        use HasFactory;
        protected $fillable = [
            'setting_key',
            'setting_type',
            'setting_value',
        ];
    }

控制器

    <?php

    namespace App\Http\Controllers\API;

    use Response;
    use Storage;
    use Log;
    use Cache;
    use App\Http\Controllers\Controller;
    use Illuminate\Http\Request;
    use App\Models\Setting;
    class EmailSettingsController extends Controller
    {
        private $keys = [
            "MAIL_MAILER",
            "MAIL_HOST",
            "MAIL_PORT",
            "MAIL_USERNAME",
            "MAIL_PASSWORD",
            "MAIL_ENCRYPTION",
            "MAIL_FROM_ADDRESS",
            "MAIL_FROM_NAME",
        ];
        public function index(Request $request){
            $emailSettings = ["MAIL_MAILER"=>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. 中间件 通过中间件批改对应路由下的邮箱配置
    <?php

    namespace App\Http\Middleware;

    use Closure;
    use Illuminate\Http\Request;
    use App\Models\Setting;
    class EmailSettingMiddleware
    {
        /**
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * @return mixed
         */
        public function handle(Request $request, Closure $next)
        {
            $keys = [
                "MAIL_MAILER"=>"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);
        }
    }

正文完
 0