Ill introduce how to make a simple email registration with Yii Forms and using an email extension for yii.
If you haven’t installed any email extensions check this post Yii email extension
1 2 3 4 5 6 7 8 |
CREATE TABLE JUser( userid int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, username varchar(40) NOT NULL, email varchar(40) NOT NULL, activation varchar(40) NOT NULL ) |
If you dont know what gii is check here Gii
To create the form you will need the user model which also can be created with gii, as long as you have the table in your database
Find and change the part in main.php under views/layouts to look like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
... <div id="mainmenu"> <?php $this->widget('zii.widgets.CMenu',array( 'items'=>array( array('label'=>'Home', 'url'=>array('/site/index')), array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')), array('label'=>'Contact', 'url'=>array('/site/contact')), array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest), array('label'=>'Register', 'url'=>array('/site/register')), array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest) ), )); ?> </div><!-- mainmenu --> ... |
For simplicity I’ll ask for a username and email to register, Adding other fields is pretty easy with Yii.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
public function actionRegister() { $model = new JUser('register'); if(isset($_POST['JUser'])) { $model->attributes = $_POST['JUser']; if($model->validate()) { $activation = md5(uniqid(rand(), true)); //Check to see if the email is already taken if( $model->findByAttributes( array('email'=>$model->email) )){ $this->renderText('That email is already taken!',true); }else { // Send the email: $message_part1 = "Please click on this link to Activate your Account :nn"; $message_part2 = "http://localhost/myapp/index.php?r=site/activate" . '?email=' . urlencode($model->email) . '&activation='. $activation; $yiimail = new YiiMailMessage; $yiimail->setBody($message_part1 . $message_part2,'text/html'); $yiimail->subject = 'Service'; $yiimail->addTo('myemail@email.com'); $yiimail->from = 'myservice@email.com'; Yii::app()->mail->send($yiimail); $model->activation = $activation; $model->save(); $this->renderText('Click on the Activation Link in your email to Activate your account',true); } } } //reset modell $model = new JUser('activation'); $this->render('register',array('model'=>$model)); } |
Notice that these fields are hidden and will get their values from the Url, on submit the activation action will be called
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<div class="form"> <?php $form=$this->beginWidget('CActiveForm', array( 'id'=>'juser-activate-form', 'enableAjaxValidation'=>false, )); ?> <p class="note">Press the button to activate your Account</p> <?php echo $form->errorSummary($model); ?> <?php echo $form->hiddenField($model,'activation',array('type'=>"hidden",'value'=>Yii::app()->getRequest()->getQuery('activation'))); ?> <?php echo $form->hiddenField($model,'email',array('type'=>"hidden",'value'=>Yii::app()->getRequest()->getQuery('email'))); ?> <div class="row buttons"> <?php echo CHtml::submitButton('Submit'); ?> </div> <?php $this->endWidget(); ?> </div><!-- form --> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public function actionActivate() { $model = new JUser('activation'); if(isset($_POST['JUser'])) { $model->attributes=$_POST['JUser']; $model = $model->findByAttributes( array('email'=>$model->email,'activation'=>$model->activation) ); if( $model ){ $model->activation = null; $model->save(); } } //reset modell $model = new JUser('activation'); $this->render('activate',array('model'=>$model)); } |