SiteController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace frontend\controllers;
  3. use Yii;
  4. use yii\base\InvalidParamException;
  5. use yii\web\BadRequestHttpException;
  6. use yii\web\Controller;
  7. use yii\filters\VerbFilter;
  8. use yii\filters\AccessControl;
  9. use common\models\LoginForm;
  10. use frontend\models\PasswordResetRequestForm;
  11. use frontend\models\ResetPasswordForm;
  12. use frontend\models\SignupForm;
  13. use frontend\models\ContactForm;
  14. /**
  15. * Site controller
  16. */
  17. class SiteController extends Controller
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function behaviors()
  23. {
  24. return [
  25. 'access' => [
  26. 'class' => AccessControl::className(),
  27. 'only' => ['logout', 'signup'],
  28. 'rules' => [
  29. [
  30. 'actions' => ['signup'],
  31. 'allow' => true,
  32. 'roles' => ['?'],
  33. ],
  34. [
  35. 'actions' => ['logout'],
  36. 'allow' => true,
  37. 'roles' => ['@'],
  38. ],
  39. ],
  40. ],
  41. 'verbs' => [
  42. 'class' => VerbFilter::className(),
  43. 'actions' => [
  44. 'logout' => ['post'],
  45. ],
  46. ],
  47. ];
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function actions()
  53. {
  54. return [
  55. 'error' => [
  56. 'class' => 'yii\web\ErrorAction',
  57. ],
  58. 'captcha' => [
  59. 'class' => 'yii\captcha\CaptchaAction',
  60. 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  61. ],
  62. ];
  63. }
  64. /**
  65. * Displays homepage.
  66. *
  67. * @return mixed
  68. */
  69. public function actionIndex()
  70. {
  71. $test = '789';
  72. $redis = Yii::$app->cache;
  73. $redis->set('test', $test);
  74. //Yii::$app->redis->get('test');
  75. exit('成功');
  76. return $this->render('index');
  77. }
  78. /**
  79. * Logs in a user.
  80. *
  81. * @return mixed
  82. */
  83. public function actionLogin()
  84. {
  85. if (!Yii::$app->user->isGuest) {
  86. return $this->goHome();
  87. }
  88. $model = new LoginForm();
  89. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  90. return $this->goBack();
  91. } else {
  92. $model->password = '';
  93. return $this->render('login', [
  94. 'model' => $model,
  95. ]);
  96. }
  97. }
  98. /**
  99. * Logs out the current user.
  100. *
  101. * @return mixed
  102. */
  103. public function actionLogout()
  104. {
  105. Yii::$app->user->logout();
  106. return $this->goHome();
  107. }
  108. /**
  109. * Displays contact page.
  110. *
  111. * @return mixed
  112. */
  113. public function actionContact()
  114. {
  115. $model = new ContactForm();
  116. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  117. if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
  118. Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
  119. } else {
  120. Yii::$app->session->setFlash('error', 'There was an error sending your message.');
  121. }
  122. return $this->refresh();
  123. } else {
  124. return $this->render('contact', [
  125. 'model' => $model,
  126. ]);
  127. }
  128. }
  129. /**
  130. * Displays about page.
  131. *
  132. * @return mixed
  133. */
  134. public function actionAbout()
  135. {
  136. return $this->render('about');
  137. }
  138. /**
  139. * Signs user up.
  140. *
  141. * @return mixed
  142. */
  143. public function actionSignup()
  144. {
  145. $model = new SignupForm();
  146. if ($model->load(Yii::$app->request->post())) {
  147. if ($user = $model->signup()) {
  148. if (Yii::$app->getUser()->login($user)) {
  149. return $this->goHome();
  150. }
  151. }
  152. }
  153. return $this->render('signup', [
  154. 'model' => $model,
  155. ]);
  156. }
  157. /**
  158. * Requests password reset.
  159. *
  160. * @return mixed
  161. */
  162. public function actionRequestPasswordReset()
  163. {
  164. $model = new PasswordResetRequestForm();
  165. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  166. if ($model->sendEmail()) {
  167. Yii::$app->session->setFlash('success', 'Check your email for further instructions.');
  168. return $this->goHome();
  169. } else {
  170. Yii::$app->session->setFlash('error', 'Sorry, we are unable to reset password for the provided email address.');
  171. }
  172. }
  173. return $this->render('requestPasswordResetToken', [
  174. 'model' => $model,
  175. ]);
  176. }
  177. /**
  178. * Resets password.
  179. *
  180. * @param string $token
  181. * @return mixed
  182. * @throws BadRequestHttpException
  183. */
  184. public function actionResetPassword($token)
  185. {
  186. try {
  187. $model = new ResetPasswordForm($token);
  188. } catch (InvalidParamException $e) {
  189. throw new BadRequestHttpException($e->getMessage());
  190. }
  191. if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
  192. Yii::$app->session->setFlash('success', 'New password saved.');
  193. return $this->goHome();
  194. }
  195. return $this->render('resetPassword', [
  196. 'model' => $model,
  197. ]);
  198. }
  199. }