RegistrationController.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 16/7/26
  6. * Time: 下午4:17
  7. */
  8. namespace common\modules\user\frontend\controllers;
  9. use common\modules\user\models\SignupForm;
  10. use common\modules\user\models\User;
  11. use Yii;
  12. use yii\web\Controller;
  13. use yii\web\NotFoundHttpException;
  14. use yii\web\Response;
  15. use yii\widgets\ActiveForm;
  16. class RegistrationController extends Controller
  17. {
  18. /**
  19. * Signs user up.
  20. *
  21. * @return mixed
  22. */
  23. public function actionSignup()
  24. {
  25. $model = new SignupForm();
  26. if ($model->load(Yii::$app->request->post())) {
  27. if (Yii::$app->request->isAjax) {
  28. Yii::$app->response->format = Response::FORMAT_JSON;
  29. return ActiveForm::validate($model);
  30. }
  31. if ($user = $model->signup()) {
  32. if (Yii::$app->getUser()->login($user)) {
  33. return $this->goHome();
  34. }
  35. }
  36. }
  37. return $this->render('signup', [
  38. 'model' => $model,
  39. 'module' => $this->module
  40. ]);
  41. }
  42. public function actionConfirm($id, $code)
  43. {
  44. $user = User::findIdentity($id);
  45. if ($user === null || $this->module->enableConfirmation == false) {
  46. throw new NotFoundHttpException();
  47. }
  48. list($success, $message) = $user->attemptConfirmation($code);
  49. Yii::$app->session->setFlash($success ? 'success' : 'error', $message);
  50. return $this->redirect(['settings/basic']);
  51. }
  52. }