Yii2如何在不使用站点/注册的情况下自动注册新用户

时间:2017-09-03 20:36:54

标签: yii2 save yii2-advanced-app autosave

我想在每次添加新客户端时注册新用户。我不想通过浏览器使用网站/网站/注册表单手动注册新用户。 我试图从ClientController自动注册新用户,我根本没有运气。没有返回错误,就好像代码根本没有运行一样。 我可以通过站点/注册站点控制器轻松注册同一个用户,但不能通过调用SignupForm模型自动注册另一个控制器。请帮忙 我在我的客户端控制器中有这个

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
                               ]);
        }
    }
}

这是我的SignupForm代码:

<?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 个答案:

没有答案