Yii2 How to auto register new user without using s

2019-08-20 08:25发布

I want to signup new user every time I add a new client. I do not want to manually register a new user using the web/site/signup form via the browser. I am trying to auto register new users from ClientController and I am having no luck at all. No error returned, it is as if the code doesn't run at all. I can easily register the same user through site controller at site/signup but can not at all auto register through another controller by calling SignupForm model. Please help I have this in my client controller

namespace frontend\controllers;
use Yii;
use yii\web\Controller;
use yii\helpers\Html;
use frontend\models\Client;
use frontend\models\SignupForm;

class ClientController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                'delete' => ['post'],
                ],
            ],
        ];
    }
public function actionCreate()
    {
       $model = new Client;
       if ($model->load(Yii::$app->request->post()))
         {
           $model->save();
           $useremail = $model->Email;

           $signupForm = new SignupForm();
           $signupForm->username = $useremail;
           $signupForm->password = $useremail;
           $signupForm->email = $useremail;
           $signupForm->created_at = time();
           $signupForm->picture = null;
           $signupForm->created_by = Yii::$app->session->get('USER_ID');
           $NewUser = $signupForm->signup();
           print_r($NewUser);
                        exit;


        }
    else{
           return $this->render('create', [
                                  'model' => $model
                               ]);
        }
    }
}

and this is my SignupForm code:

<?php
namespace frontend\models;
use common\models\User;
use yii\base\Model;
use Yii;
/**
* Signup form
*/
class SignupForm extends Model
{
public $username;
public $email;
public $password;
public $picture;
public $created_by;
/**
 * @inheritdoc
 */

public function rules()
{
    return [
                ['username', 'filter', 'filter' => 'trim'],
                ['username', 'required'],
                ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
                ['username', 'string', 'min' => 2, 'max' => 255],

                ['email', 'filter', 'filter' => 'trim'],
                ['email', 'required'],
                ['email', 'email'],
                ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],

                ['password', 'required'],
                ['password', 'string', 'min' => 6],
                [['picture'], 'string'],
                [['picture'],'default', 'value'=>null],
                [['created_by'], 'integer'],
                [['created_by'], 'default', 'value'=>Yii::$app->session->get('USER_ID')],
            ];
}


/**
 * Signs user up.
 *
 * @return User|null the saved model or null if saving fails
 */
public function signup()
{

    $this->created_by = Yii::$app->session->get('USER_ID');
    if ($this->validate()) {
        $user = new User();

        $user->username = $this->username;
        $user->email = $this->email;
        $user->setPassword($this->password);
        $user->generateAuthKey();
        $user->created_by = $this->created_by;
        $user->picture = $this->picture;
        //Save user
        $user->save(false);
        return $user;
    }

    return null;
}

}

0条回答
登录 后发表回答