Exemplo n.º 1
0
 /**
  * Creates a new User model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new User(['scenario' => 'create']);
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Exemplo n.º 2
0
 /**
  * Creates a new User model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  *
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new User(['scenario' => User::SCENARIO_CREATE]);
     if (Yii::$app->request->isPost) {
         // Traitement du formulaire
         if (!($model->load(Yii::$app->request->post()) && $model->save())) {
             // On ré-affiche le formulaire avec ses erreurs
             Yii::$app->session->setFlash('flash-warning', HLib::t('messages', 'There are errors in your form'));
         } else {
             // Retour à la liste ou redirection sur la page d'édition, selon le bouton qui a été cliqué
             Yii::$app->session->setFlash('flash-success', HLib::t('messages', 'Create successful'));
             $requestedRedirection = Yii::$app->request->getBodyParam('action') == 'saveAndKeepEditing' ? Url::toRoute(['/users/users/update', 'id' => $model->id]) : Url::toRoute('/users/users/index');
             return $this->redirect($requestedRedirection);
         }
     }
     // Affichage ou ré-affichage
     return $this->render('create', compact('model'));
 }
Exemplo n.º 3
0
 /**
  * Creates a new User model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $user = new User(['scenario' => 'create']);
     $profile = new Profile();
     if ($user->load(Yii::$app->request->post()) && $profile->load(Yii::$app->request->post())) {
         if ($user->validate() && $profile->validate()) {
             //$user->populateRelation('profile', $profile);
             if ($user->save(false)) {
                 $user->link('profile', $profile);
                 Yii::$app->session->setFlash('success', Module::t('users', 'User has been successfully created.'));
                 return $this->redirect(['update', 'id' => $user->id]);
             } else {
                 Yii::$app->session->setFlash('danger', Module::t('users', 'User has not been saved. Please try again!'));
                 return $this->refresh();
             }
         }
     }
     return $this->render('create', ['user' => $user, 'profile' => $profile]);
 }
Exemplo n.º 4
0
 /**
  * Авторизуем пользователя.
  */
 public function actionLogin()
 {
     // В случае если пользователь не гость, то мы перенаправляем его на главную страницу. В противном случае он бы увидел 403-ю ошибку.
     if (!Yii::$app->user->isGuest) {
         $this->goHome();
     }
     $model = new User(['scenario' => 'login']);
     if ($model->load(Yii::$app->request->post())) {
         $user = User::findByUsername($model->login);
         if ($user && $user->validatePassword($model->password)) {
             Yii::$app->user->login($user, 3600 * 24 * 30);
             return $this->goBack();
         } else {
             $model->addError('', 'Неверно введен логин или пароль');
             return $this->render('login', ['model' => $model]);
         }
     }
     // Рендерим представление.
     return $this->render('login', ['model' => $model]);
 }