P
P
ProFM2018-06-17 08:55:16
Yii
ProFM, 2018-06-17 08:55:16

How to make a redirect in yii2 after user registration?

Good afternoon!
I can't figure out what's going on with Yii2..... I'm still learning, so don't be too angry with me.
I took a code from another site, it worked well, a person registered, entered data, pressed a button, and he was redirected to the main page and immediately authorized, this was the code:

SignupForm
public function signup()
    {
        if (!$this->validate()) {
            return null;
        }

        $user = new User();
        $user->username = $this->username;
        $user->email = $this->email;
        $user->setPassword($this->password);
        $user->generateAuthKey();
        return $user->save() ? $user : null;

SiteController
public function actionSignup()
    {
        $model = new SignupForm();
        if ($model->load(Yii::$app->request->post())) {
            if ($user = $model->signup()) {
                if (Yii::$app->getUser()->login($user)) {
                    return $this->goHome();
                }
            }
        }
        return $this->render('signup', [
            'model' => $model,
        ]);
    }

Everything worked great. Then I needed to add a role to the RBAC tables during registration, Maxim Timofeev helped me for which I am very grateful to him, everything was added to the database and to user and RBAC, the code looked like this:
SignupForm
public function signup()
    {
        if (!$this->validate()) {
            return null;
        }

        $user = new User();
        $user->username = $this->username;
        $user->email = $this->email;
        $user->setPassword($this->password);
        $user->generateAuthKey();
        if($user->save()){
            $role = Yii::$app->authManager->getRole('user');
            Yii::$app->authManager->assign($role, $user->id_user);
        }
    }

Well, SiteController is unchanged. As a result, the redirect stopped working, and I understand that the script does not enter this piece of code
if ($user = $model->signup()) {
   if (Yii::$app->getUser()->login($user)) {
       return $this->goHome();
   }
}

and this is how it redirects normally to the main one, but does not authorize the user.
if ($user = $model->signup()) {
    if (Yii::$app->getUser()->login($user)) {
          return $this->goHome();
     }
}
return $this->goHome();

those. bypasses the first part of the code.
Please help, I really want to figure it out, make a good site, but so far everything is hard to come by. Don't hit hard

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
ProFM, 2018-06-17
@ProFM

In short, as always, I did such a stupid thing ..... I didn’t add return to the form model ....... I wrote here, looked again and everything came up, sorry for the trouble.
If someone needs an answer, then here it is, you just had to add return to

SignupForm
public function signup()
    {
        if (!$this->validate()) {
            return null;
        }

        $user = new User();
        $user->username = $this->username;
        $user->email = $this->email;
        $user->setPassword($this->password);
        $user->generateAuthKey();
        if($user->save()){
            $role = Yii::$app->authManager->getRole('user');
            Yii::$app->authManager->assign($role, $user->id_user);
        }
        return $user;
    }

D
Dmitry, 2018-06-17
@slo_nik

Good morning.
And you don't return anything in the signUp() method, that's why it doesn't work.
Compare what you had with what you have now.
In the first option you returned In the second option you return nothing

if($user->save()){
            $role = Yii::$app->authManager->getRole('user');
            Yii::$app->authManager->assign($role, $user->id_user);
        }

Try it like this
if($user->save()){
    $role = Yii::$app->authManager->getRole('user');
    Yii::$app->authManager->assign($role, $user->id_user);
     return true;
}
else{
  return false;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question