SiteController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2017-03-15 21:16
  7. */
  8. namespace frontend\controllers;
  9. use Yii;
  10. use frontend\models\form\SignupForm;
  11. use frontend\models\form\LoginForm;
  12. use frontend\models\form\PasswordResetRequestForm;
  13. use frontend\models\form\ResetPasswordForm;
  14. use yii\base\InvalidParamException;
  15. use yii\helpers\Html;
  16. use yii\web\BadRequestHttpException;
  17. use yii\web\Controller;
  18. use yii\filters\VerbFilter;
  19. use yii\filters\AccessControl;
  20. use yii\web\HttpException;
  21. /**
  22. * Site controller
  23. */
  24. class SiteController extends Controller
  25. {
  26. /**
  27. * @inheritdoc
  28. */
  29. public function behaviors()
  30. {
  31. return [
  32. 'access' => [
  33. 'class' => AccessControl::className(),
  34. 'only' => ['logout', 'signup'],
  35. 'rules' => [
  36. [
  37. 'actions' => ['signup'],
  38. 'allow' => true,
  39. 'roles' => ['?'],
  40. ],
  41. [
  42. 'actions' => ['logout'],
  43. 'allow' => true,
  44. 'roles' => ['@'],
  45. ],
  46. ],
  47. ],
  48. 'verbs' => [
  49. 'class' => VerbFilter::className(),
  50. 'actions' => [
  51. 'logout' => ['post', 'get'],
  52. ],
  53. ],
  54. ];
  55. }
  56. /**
  57. * Logs in a user.
  58. *
  59. * @return mixed
  60. */
  61. public function actionLogin()
  62. {
  63. if (! Yii::$app->getUser()->getIsGuest()) {
  64. return $this->goHome();
  65. }
  66. $model = new LoginForm();
  67. if ($model->load(Yii::$app->getRequest()->post()) && $model->login()) {
  68. return $this->goBack();
  69. } else {
  70. Yii::$app->getUser()->setReturnUrl(Yii::$app->getRequest()->getHeaders()->get('referer'));
  71. return $this->render('login', [
  72. 'model' => $model,
  73. ]);
  74. }
  75. }
  76. /**
  77. * Logs out the current user.
  78. *
  79. * @return mixed
  80. */
  81. public function actionLogout()
  82. {
  83. Yii::$app->getUser()->logout(false);
  84. return $this->goHome();
  85. }
  86. /**
  87. * Signs user up.
  88. *
  89. * @return mixed
  90. * @throws yii\base\Exception
  91. */
  92. public function actionSignup()
  93. {
  94. $model = new SignupForm();
  95. if ($model->load(Yii::$app->getRequest()->post())) {
  96. if ($user = $model->signup()) {
  97. if (Yii::$app->getUser()->login($user)) {
  98. return $this->goHome();
  99. }
  100. }
  101. }
  102. return $this->render('signup', [
  103. 'model' => $model,
  104. ]);
  105. }
  106. /**
  107. * Requests password reset.
  108. *
  109. * @return mixed
  110. */
  111. public function actionRequestPasswordReset()
  112. {
  113. $model = new PasswordResetRequestForm();
  114. if ($model->load(Yii::$app->getRequest()->post()) && $model->validate()) {
  115. if ($model->sendEmail()) {
  116. Yii::$app->getSession()
  117. ->setFlash('success', Yii::t('app', 'Check your email for further instructions.'));
  118. return $this->goHome();
  119. } else {
  120. Yii::$app->getSession()
  121. ->setFlash('error', 'Sorry, we are unable to reset password for email provided.');
  122. }
  123. }
  124. return $this->render('requestPasswordResetToken', [
  125. 'model' => $model,
  126. ]);
  127. }
  128. /**
  129. * Resets password.
  130. *
  131. * @param string $token
  132. * @return mixed
  133. * @throws BadRequestHttpException
  134. */
  135. public function actionResetPassword($token)
  136. {
  137. try {
  138. $model = new ResetPasswordForm($token);
  139. } catch (InvalidParamException $e) {
  140. throw new BadRequestHttpException($e->getMessage());
  141. }
  142. if ($model->load(Yii::$app->getRequest()->post()) && $model->validate() && $model->resetPassword()) {
  143. Yii::$app->getSession()->setFlash('success', Yii::t('app', 'New password was saved.'));
  144. return $this->goHome();
  145. }
  146. return $this->render('resetPassword', [
  147. 'model' => $model,
  148. ]);
  149. }
  150. /**
  151. * website maintain shows page
  152. * when at "/admin/index.php?r=site/website" change website status to closed every request will execute this action
  153. */
  154. public function actionOffline()
  155. {
  156. Yii::$app->getResponse()->statusCode = 503;
  157. return "sorry, the site is temporary unserviceable";
  158. }
  159. /**
  160. * change view template
  161. * development website template first,then config according to yii2 document
  162. */
  163. public function actionView()
  164. {
  165. $view = Yii::$app->getRequest()->get('type', null);
  166. if (isset($view) && !empty($view)) {
  167. Yii::$app->session['view'] = $view;
  168. }
  169. $this->goBack( Yii::$app->getRequest()->getReferrer() );
  170. }
  171. /**
  172. * change language
  173. */
  174. public function actionLanguage()
  175. {
  176. $language = Yii::$app->getRequest()->get('lang');
  177. if (isset($language)) {
  178. $session = Yii::$app->getSession();
  179. $session['language'] = Html::encode($language);
  180. }
  181. $this->redirect( Yii::$app->getRequest()->getReferrer() );
  182. }
  183. /**
  184. * exception handler
  185. *
  186. * @return string
  187. */
  188. public function actionError()
  189. {
  190. if (($exception = Yii::$app->getErrorHandler()->exception) === null) {
  191. // action has been invoked not from error handler, but by direct route, so we display '404 Not Found'
  192. $exception = new HttpException(404, Yii::t('yii', 'Page not found.'));
  193. }
  194. if ($exception instanceof HttpException) {
  195. $code = $exception->statusCode;
  196. } else {
  197. $code = $exception->getCode();
  198. }
  199. //if ($exception instanceof Exception) {
  200. $name = $exception->getName();
  201. //} else {
  202. //$name = $this->defaultName ?: Yii::t('Yii', 'Error');
  203. //}
  204. if ($code) {
  205. $name .= " (#$code)";
  206. }
  207. //if ($exception instanceof UserException) {
  208. $message = $exception->getMessage();
  209. //} else {
  210. //$message = $this->defaultMessage ?: Yii::t('Yii', 'An internal server error occurred.');
  211. //}
  212. $statusCode = $exception->statusCode ? $exception->statusCode : 500;
  213. if (Yii::$app->getRequest()->getIsAjax()) {
  214. return "$name: $message";
  215. } else {
  216. return $this->render('error', [
  217. 'code' => $statusCode,
  218. 'name' => $name,
  219. 'message' => $message,
  220. 'exception' => $exception,
  221. ]);
  222. }
  223. }
  224. }