Moving signup to the backend

Just started with Yii2 and have (another) question I want to be able to create new users with the backend. I tried moving the signup.php and change some things but it didn’t work. Problems getting the model from for instance the SiteController to the “view” signup.php…

public function actionSignup()


{


	$model = new User();


	


    return $this->render('signup', [


        'model' => $model,


    ]);


}

I then started with gii to create a new model and a CRUD. I changed the "form_.php"

<?php

use yii\helpers\Html;

use yii\widgets\ActiveForm;

/* @var $this yii\web\View */

/* @var $model backend\models\User */

/* @var $form yii\widgets\ActiveForm */

?>

<div class="user-form">

&lt;?php &#036;form = ActiveForm::begin(); ?&gt;





&lt;?= &#036;form-&gt;field(&#036;model, 'username')-&gt;textInput(['maxlength' =&gt; 255]) ?&gt;





&lt;?= &#036;form-&gt;field(&#036;model, 'email')-&gt;textInput(['maxlength' =&gt; 255]) ?&gt;





&lt;?= &#036;form-&gt;field(&#036;model, 'password')-&gt;passwordInput() ?&gt;





&lt;?= &#036;form-&gt;field(&#036;model, 'status')-&gt;textInput() ?&gt;








&lt;div class=&quot;form-group&quot;&gt;


    &lt;?= Html::submitButton(&#036;model-&gt;isNewRecord ? 'Create' : 'Update', ['class' =&gt; &#036;model-&gt;isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?&gt;


&lt;/div&gt;





&lt;?php ActiveForm::end(); ?&gt;

</div>

and my user model:

<?php

namespace backend\models;

use Yii;

/**

  • This is the model class for table "user".

  • @property integer $id

  • @property string $username

  • @property string $auth_key

  • @property string $password_hash

  • @property string $password_reset_token

  • @property string $email

  • @property integer $status

  • @property integer $created_at

  • @property integer $updated_at

*/

class User extends \yii\db\ActiveRecord

{

/**


 * @inheritdoc


 */


public static function tableName()


{


    return 'user';


}





public function rules()


{


    return [


        ['username', 'filter', 'filter' =&gt; 'trim'],


        ['username', 'required'],


        ['username', 'unique', 'targetClass' =&gt; '&#092;common&#092;models&#092;User', 'message' =&gt; 'This username has already been taken.'],


        ['username', 'string', 'min' =&gt; 2, 'max' =&gt; 255],





        ['email', 'filter', 'filter' =&gt; 'trim'],


        ['email', 'required'],


        ['email', 'email'],


        ['email', 'unique', 'targetClass' =&gt; '&#092;common&#092;models&#092;User', 'message' =&gt; 'This email address has already been taken.'],





        ['password', 'required'],


        ['password', 'string', 'min' =&gt; 6],


    ];


}





/**


 * Signs user up.


 *


 * @return User|null the saved model or null if saving fails


 */


public function signup()


{


    if (&#036;this-&gt;validate()) {


        &#036;user = new User();


        &#036;user-&gt;username = &#036;this-&gt;username;


        &#036;user-&gt;email = &#036;this-&gt;email;


        &#036;user-&gt;setPassword(&#036;this-&gt;password);


        &#036;user-&gt;generateAuthKey();


        if (&#036;user-&gt;save()) {


            return &#036;user;


        }


    }





    return null;


}

}

?>

But when I go to my application I get this error:

Getting unknown property: backend\models\User::password

It’s a bit hard to figure out when your just starting…

It’s saying there’s no “password” property in backend\models\User model and I guess it’s true. User model that’s located in common namespace has a setter method for password.

I switched to common but now I get:

Getting write-only property: common\models\User::password

I was able to move the signup.php to the backend and get it to work. I copied below to the UserController.php

added:

use frontend\models\SignupForm;

public function actionSignup()

{


    &#036;model = new SignupForm();


    if (&#036;model-&gt;load(Yii::&#036;app-&gt;request-&gt;post())) {


        if (&#036;user = &#036;model-&gt;signup()) {


            if (Yii::&#036;app-&gt;getUser()-&gt;login(&#036;user)) {


                return &#036;this-&gt;goHome();


            }


        }


    }





    return &#036;this-&gt;render('signup', [


        'model' =&gt; &#036;model,


    ]);


}

Thx for your help.