DefaultController.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace common\modules\config\backend\controllers;
  3. use common\modules\config\models\Config;
  4. use common\modules\config\models\DatabaseConfigForm;
  5. use common\modules\config\models\MailConfigForm;
  6. use Yii;
  7. use yii\web\Controller;
  8. use yii\base\Model;
  9. use yii\caching\TagDependency;
  10. /**
  11. * ConfigController implements the CRUD actions for Config model.
  12. */
  13. class DefaultController extends Controller
  14. {
  15. public function actionIndex()
  16. {
  17. $groups = Yii::$app->config->get('config_group');
  18. $group = Yii::$app->request->get('group', current(array_keys($groups)));
  19. $configModels = Config::find()->where(['group' => $group])->all();
  20. return $this->render('index', [
  21. 'groups' => $groups,
  22. 'group' => $group,
  23. 'configModels' => $configModels
  24. ]);
  25. }
  26. public function actionStore()
  27. {
  28. $groups = Yii::$app->config->get('config_group');
  29. $group = Yii::$app->request->get('group', current(array_keys($groups)));
  30. $configModels = Config::find()->where(['group' => $group])->all();
  31. if (Model::loadMultiple($configModels, \Yii::$app->request->post()) && Model::validateMultiple($configModels)) {
  32. foreach ($configModels as $configModel) {
  33. /* @var $config Config */
  34. $configModel->save(false);
  35. }
  36. TagDependency::invalidate(\Yii::$app->cache, Yii::$app->config->cacheTag);
  37. Yii::$app->session->setFlash('success', '保存成功');
  38. return $this->redirect(['index', 'group' => $group]);
  39. } else {
  40. Yii::$app->session->setFlash('error', '保存失败');
  41. return $this->redirect(['index', 'group' => $group]);
  42. }
  43. }
  44. public function actionDatabase()
  45. {
  46. $model = new DatabaseConfigForm();
  47. $model->loadDefaultValues();
  48. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  49. return $this->redirect('database');
  50. }
  51. return $this->render('database', [
  52. 'model' => $model
  53. ]);
  54. }
  55. public function actionMail()
  56. {
  57. $model = new MailConfigForm();
  58. $model->loadDefaultValues();
  59. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  60. return $this->redirect('mail');
  61. }
  62. return $this->render('mail', [
  63. 'model' => $model
  64. ]);
  65. }
  66. }