[心得] Yii + jQuery Mobile

看板PHP作者 (cokellen)時間12年前 (2013/04/03 00:16), 編輯推噓4(400)
留言4則, 4人參與, 最新討論串1/1
今年初忽然想找一個PHP Framework來學習, 選擇了Yii下手, 意外的 還算好上手, 最近在開發Mobile Web平台, 看到jQuery Mobile的效果很好 , 決定將兩個東西一起使用, 雖然有不少衝突需要花時間解決, 但是整體 效果還不錯 ; 當初在這版看到網友[aaroms]分享了Yii的教學, 確實幫到 我不少, 以他的文章繼續下去, 分享一些Yii的使用心得 1.多國語言 使用yiic webapp 建立專案後, 可以在protected內看到有資料夾 messages, 在這裡面以資料夾做語言的區隔, 例如建立(zhtw),(enus),共 兩個資料夾, 接著建立相同名稱的php檔案分別存放這兩個資料夾內, 例如 login.php, 程式內容就是代碼對應文字 zhtw/login.php <?php return array( 't_account'=>'帳號', 't_password'=>'密碼', 't_required'=>'請勿空白' ); ?> enus/login.php <?php retunr array( 't_account'=>'Account', 't_password'=>'Password', 't_required'=>' Is Required' ); ?> 接著在config/main.php設定網站預設的語言, 使用Yii::app()->language 例如: if(empty($_GET['lang'])){ $lang = 'zhtw'; } else{ $lang = $_GET['lang']; } Yii::app()->language = $lang; 最後利用<?php echo Yii::t('login', "t_account");?> 會根據使用者所選擇的語言, 印出[帳號] or [Account] 2.AJAX欄位檢查 在protected/models內存放自訂的Form Class, 針對要使用的資料欄位 , 可以設定AJAX的檢查, 首先宣告會使用到的欄位, 以登入的表格來說明, 例如在 LoginForm.php 中宣告[帳號], [密碼]兩個變數 public account; public password; 設定變數的檢查條件, 這邊先以[required 必填]做舉例, message則是設定 欄位空白時, 所要顯示的錯誤訊息 public function rules(){ return array( array('account, password,', 'required', 'message'=>'{attribute}'.Yii::t('login', "t_required")), ); } 這邊錯誤訊息使用{attribute}, 會自動判斷是哪個變數 (因為有account, password兩個以上的變數), 當前端判斷[帳號]空白時, 會顯示 [account請勿空白], {attribute}只能抓到變數的名稱, 所以要另外設定 文字標籤來做多國語言的顯示 接著是文字標籤的設定 public function attributeLabels(){ return array( 'account'=>Yii::t('login', "t_account"), 'password'=>Yii::t('login', "t_password"), ); } 設定完後, 原先的[account請勿空白]中的account就會根據語言做變化, 例如變成[帳號請勿空白] 在protected/controller中設定每個action(動作), 定義Model以及 要顯示的View, 例如 public function actionLogIn() { $model = new LoginForm; $this->render('login',array('model'=>$model)); } 最後, 在protected/view/site/login.php, 利用Yii的語法產生form以及 資料欄位, 像是text, password, radio, hidden ....等 <?php $form=$this->beginWidget('CActiveForm', array( 'id'=>'login-form', 'enableClientValidation'=>true, 'clientOptions'=>array( 'validateOnSubmit'=>true, ) )); ?> <?php echo $form->textField($model,'account');?> <?php echo $form->error($model,'account'); ?> <input type="submit" value="submit"> <?php $this->endWidget(); ?> 在按下Submit送出的時候, 就會根據在LoginForm中的設定欄位檢查條件 去做檢查, 並且透過 echo $form->error($model,'account')將設定好的 錯誤訊息印出 ---------------------------------------------------------------- LoginForm的欄位檢查條件function rules()中, 除了[required必填], 還有 其他一些常用的檢查條件 A. 正規表示法 array('account, password', 'match', 'pattern'=>'/^[A-Za-z0-9]{6,12}$/', 'message'=>Yii::t('login', "e_match")), B. 兩個欄位資料比對(例如設定密碼, 需要 [密碼] & [確認密碼] ), 需要注意的是, 兩個變數的位置 array('password_confirm', 'compare', 'compareAttribute'=>'password', 'message'=>Yii::t('login', "e_pass")), 其它的檢查條件可以至yii\framework\validators中查看 先這樣.... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 1.169.241.218

04/03 00:41, , 1F
感謝分享
04/03 00:41, 1F

04/03 01:40, , 2F
推~
04/03 01:40, 2F

04/03 13:54, , 3F
推!!
04/03 13:54, 3F

04/05 23:53, , 4F
感謝!最近也對jQuery Mobile有興趣~歡迎多多分享教學!
04/05 23:53, 4F
文章代碼(AID): #1HMmHKdc (PHP)
文章代碼(AID): #1HMmHKdc (PHP)