Setelah kita menganalisa menu Contact Yii, maka kita masuk ke menu Login Yii.
Untuk mengakses menu login ini kita masuk ke http://yii/coba/index.php?r=site/login
- Dari link tersebut kita mengetahui bahwa controller yang dipakai adalah SiteController dan actionnya adalah actionLogin
- Kita akan membuka SiteController yang ada di ./public_html/coba/protected/controllers/SiteController.php
dan perhatikan coding berikut:
//start————————————————————————-
/**
* Displays the login page
*/
public function actionLogin()
{
$model=new LoginForm;// if it is ajax validation request
if(isset($_POST[‘ajax’]) && $_POST[‘ajax’]===’login-form’)
{
echo CActiveForm::validate($model);
Yii::app()->end();
}// collect user input data
if(isset($_POST[‘LoginForm’]))
{
$model->attributes=$_POST[‘LoginForm’];
// validate user input and redirect to the previous page if valid
if($model->validate() && $model->login())
$this->redirect(Yii::app()->user->returnUrl);
}
// display the login form
$this->render(‘login’,array(‘model’=>$model));
}//end————————————————————————–
- Perhatikan coding ‘$model=new LoginForm;’
Ini merupakan instance object dari kelas LoginForm yang ada di ./public_html/coba/protected/models/LoginForm.php yang berisi
//start————————————————————————-
/**
* LoginForm class.
* LoginForm is the data structure for keeping
* user login form data. It is used by the ‘login’ action of ‘SiteController’.
*/
class LoginForm extends CFormModel
{
public $username;
public $password;
public $rememberMe;private $_identity;
/**
* Declares the validation rules.
* The rules state that username and password are required,
* and password needs to be authenticated.
*/
public function rules()
{
return array(
// username and password are required
array(‘username, password’, ‘required’),
// rememberMe needs to be a boolean
array(‘rememberMe’, ‘boolean’),
// password needs to be authenticated
array(‘password’, ‘authenticate’),
);
}/**
* Declares attribute labels.
*/
public function attributeLabels()
{
return array(
‘rememberMe’=>’Remember me next time’,
);
}/**
* Authenticates the password.
* This is the ‘authenticate’ validator as declared in rules().
*/
public function authenticate($attribute,$params)
{
if(!$this->hasErrors())
{
$this->_identity=new UserIdentity($this->username,$this->password);
if(!$this->_identity->authenticate())
$this->addError(‘password’,’Incorrect username or password.’);
}
}/**
* Logs in the user using the given username and password in the model.
* @return boolean whether login is successful
*/
public function login()
{
if($this->_identity===null)
{
$this->_identity=new UserIdentity($this->username,$this->password);
$this->_identity->authenticate();
}
if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
{
$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
Yii::app()->user->login($this->_identity,$duration);
return true;
}
else
return false;
}
}//end————————————————————————–
- Pada langkah 1 perhatikan coding ‘$this->render(‘login’,array(‘model’=>$model));’
Ini merupakan perintah agar View menampilan content yang berada di ./public_html/coba/protected/views/site/login.php yang isinya
//start————————————————————————-
<?php
$this->pageTitle=Yii::app()->name . ‘ – Login’;
$this->breadcrumbs=array(
‘Login’,
);
?><h1>Login</h1>
<p>Please fill out the following form with your login credentials:</p>
<div class=”form”>
<?php $form=$this->beginWidget(‘CActiveForm’, array(
‘id’=>’login-form’,
‘enableAjaxValidation’=>true,
)); ?><p class=”note”>Fields with <span class=”required”>*</span> are required.</p>
<div class=”row”>
<?php echo $form->labelEx($model,’username’); ?>
<?php echo $form->textField($model,’username’); ?>
<?php echo $form->error($model,’username’); ?>
</div><div class=”row”>
<?php echo $form->labelEx($model,’password’); ?>
<?php echo $form->passwordField($model,’password’); ?>
<?php echo $form->error($model,’password’); ?>
<p class=”hint”>
Hint: You may login with <tt>demo/demo</tt> or <tt>admin/admin</tt>.
</p>
</div><div class=”row rememberMe”>
<?php echo $form->checkBox($model,’rememberMe’); ?>
<?php echo $form->label($model,’rememberMe’); ?>
<?php echo $form->error($model,’rememberMe’); ?>
</div><div class=”row buttons”>
<?php echo CHtml::submitButton(‘Login’); ?>
</div><?php $this->endWidget(); ?>
</div><!– form –>
//end————————————————————————– - Selanjutnya form akan tampil sebagai berikut:
Kunjungi www.proweb.co.id untuk menambah wawasan anda.