一、數據庫連接
1、配置連接參數
在database.php里面開啟:
'db' => array( 'connectionString' => 'mysql:host=127.0.0.1;dbname=blog', 'emulatePrepare' => true, // PDO擴展 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'tablePrefix' => 'yii_', // 表前綴 'enableParamLogging' => true // 開啟SQL調試信息 ),
更多配置在 framework/db/CDbConnection.php中 可以找到
2、測試連接
用 var_dump(Yii::app()->db);可以測試數據庫是否連接成功
二、定義模型與查詢數據
1、定義模型
位置:protected/models/
名稱:admin.php
內容:模型必須有兩個方法 —— model() 與 tableName(),如下。
class Admin extends CActiveRecord{
// 必需方法1。返回模型
public static function model($className = __CLASS__){
return parent::model($className);
}
// 必須方法2。返回表名
public function tableName(){
return "{{admin}}"; //返回yii_admin表名
}
}
2、查詢
舉個栗子,在控制器中調用用戶信息:
Admin::model()->find('username = :name' , array(':name => 'admin'));
對於返回的數據,建議打印出來看一下,方便操作每個字段。
注意:Yii的 ActiveRecord 基類模型采用了對象化,表映射到模型,記錄映射到對象,表或記錄的字段映射到模型或對象的屬性,所以用訪問屬性的方法可以訪問到字段。
三、登錄驗證
將操作以下兩個文件:
protected/models/LoginForm.php
protected/components/UserIdentity.php
參照源碼進行修改即可。
1、控制器中執行:
$loginForm = new LoginForm();
$loginForm()->login(); // 進行登錄驗證
注意:LoginForm 是 Yii 默認帶有的一個登錄模型,這個模型可以對登錄表單進行驗證,可以修改為映射到后台用戶表的 admin 模型,強迫症可以改為 admin.php,實例化的時候 new Admin() 就行。
/**
* 登錄視圖及登錄表單處理
* @return [type] [description]
*/
public function actionIndex(){
$loginForm = new LoginForm(); // 實例化LoginForm
// 登錄表單處理
if(isset($_POST['LoginForm'])){
$loginForm->attributes = $_POST['LoginForm']; // 壓入需要驗證的POST數據
if($loginForm->validate() && $loginForm->login()){ //驗證通過
Yii::app()->session['logintime'] = time(); //寫session,記錄當前登錄時間
$this->redirect(array('default/index'));
}
}
$this->render('index', array('loginForm' => $loginForm)); //渲染模板(沒有布局),分配模板變量loginForm模型
}
2、在 LoginForm模型中,rules() 方法定義規則:
public function rules()
{
return array(
array('password', 'authenticate'), //自定義 authenticate()方法驗證
);
}
public function authenticate($attribute, $params)
{
if(!$this->hasErrors())
{
$this->_identity = new UserIdentity($this->username, $this->password);
if(!$this->_identity->authenticate())
$this->addError('password','用戶名或密碼錯誤');
}
}
3、UserIdentity 類進行判斷與返回錯誤
這是它的authenticate()方法,輔助上面LoginForm模型中的authenticate()方法進行密碼驗證
public function authenticate()
{
$user= User::model()->find('username = :name', array(':name' => $this->username));
if(!$user){ // 用戶名錯誤
$this->errorCode = self::ERROR_USERNAME_INVALID;
}
else if($user->password != md5($this->password)){ // 密碼錯誤
$this->errorCode = self::ERROR_PASSWORD_INVALID;
}
else{
$this->errorCode = self::ERROR_NONE; // 驗證通過
}
return !$this->errorCode; // 真通過,假失敗
}
四、登錄信息調取
Yii::app()->user->name; // 登錄后存儲在 session 中的用戶名
注意前后台用戶區分,需要設置:(以后台模塊 admin 為例)
在 modules/admin/AdminModule.php 文件中的 init() 方法添加代碼:
Yii:app()->setComponents(array(
'user' => array('stateKeyPrefix' => 'admin')
));
五、Session使用
存儲:
Yii::app()->session['logintime'] = time();
調用:
Yii::app()->session['logintime']
清除:
Yii::app()->session->clear();
Yii::app()->session->destory();
退出登錄:
Yii::app()->user->logout(); //退出登錄,清除SESSION
六、URL跳轉與生成
URL跳轉:
$this->redirect(array('控制器名/方法名'));
URL生成:
在視圖中調用,$this->createUrl('控制器/方法', array('id' => 2));
