Yii2中的零碎知識點

PHP最佳實踐

1 PHP獲取時間戳:echo  time();

時間戳轉換 date('Y-m-d H:i:s', $時間戳);

2 linux 顯示命令 ls 顯示所有文件夾

  查看命令:tail -200 runtime/logs/app.log

  退出vim   :qz  :xa

3 數據庫日誌: tail -f /database/mysql/query.log 

composer安裝

compose官網

https://getcomposer.org/download/

在PHP環境下執行命令

php -r "readfile('https://getcomposer.org/installer');" | php

執行成功

 Yii2.0 中配置多個數據庫

1 config文件夾下 增加新的數據庫配置,格式如 passportdb.php

1
2
3
4
5
6
7
8
9
10
<span style="font-size: 14px;"><?php
 
return [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=;dbname',
    'username' => 'root',
    'password' => '',
    'charset' => 'utf8',
];?>
</span>

 2 將數據庫文件引入到程序配置文件 web.php中

 3 在model中通過 Yii::$app->passportdb;訪問數據庫

 

public static  function getDb() {
   return Yii::$app->passportdb;
}

 4 程序 如gii自動生成代碼時  通過models的 getDb() 訪問數據庫

rules常用規則

複製代碼
return array(

    //必須填寫
    array('email, username, password,agree,verifyPassword,verifyCode', 'required'),

    //檢查用戶名是否重複
    array('email','unique','message'=>'用戶名已佔用'),

    //用戶輸入最大的字符限制
    array('email, username', 'length', 'max'=>64),
    
    //限制用戶最小長度和最大長度
    array('username', 'length', 'max'=>7, 'min'=>2, 'tooLong'=>'用戶名請輸入長度爲4-14個字符', 'tooShort'=>'用戶名請輸入長度爲2-7個字'),

    //限制密碼最小長度和最大長度
    array('password', 'length', 'max'=>22, 'min'=>6, 'tooLong'=>'密碼請輸入長度爲6-22位字符', 'tooShort'=>'密碼請輸入長度爲6-22位字符'),

    //判斷用戶輸入的是否是郵件
    array('email','email','message'=>'郵箱格式錯誤'),

    //檢查用戶輸入的密碼是否是一樣的
    array('verifyPassword', 'compare', 'compareAttribute'=>'password', 'message'=>'請再輸入確認密碼'),

    //檢查用戶是否同意協議條款
    array('agree', 'required', 'requiredValue'=>true,'message'=>'請確認是否同意隱私權協議條款'),

    //判斷是否是日期格式
    array('created', 'date', 'format'=>'yyyy/MM/dd/ HH:mm:ss'),

複製代碼

 

 

複製代碼
  //判斷是否包含輸入的字符
    array('superuser', 'in', 'range' => array(0, 1)),
//正則驗證器:        
    array('name','match','pattern'=>'/^[a-z0-9\-_]+$/'),
//數字驗證器:               
    array('id', 'numerical', 'min'=>1, 'max'=>10, 'integerOnly'=>true),
 //類型驗證 integer,float,string,array,date,time,datetime                 
    array('created', 'type', 'datetime'),
//文件驗證:        
    array('filename', 'file', 'allowEmpty'=>true, 'types'=>'zip, rar, xls, pdf, ppt','tooLarge'=>'圖片不要超過800K'),

          array('url',  
            'file',    //定義爲file類型  
            'allowEmpty'=>true,   
            'types'=>'jpg,png,gif,doc,docx,pdf,xls,xlsx,zip,rar,ppt,pptx',   //上傳文件的類型  
            'maxSize'=>1024*1024*10,    //上傳大小限制,注意不是php.ini中的上傳文件大小  
            'tooLarge'=>'文件大於10M,上傳失敗!請上傳小於10M的文件!'  
        ),  
複製代碼

 

 yii2 兩種模塊的區別

 主要區別在於login機制不一樣。

基礎模板 的登錄login view文件裏 有這麼一句話【To modify the username/password, please check out the code app\models\User::$users.】 實際上可以理解爲:

基礎版默認的用戶驗證信息不是存在user表裏,存在文件models裏.

Yii2異常處理

 Yii中的錯誤處理和純PHP不同。首先,Yii將轉換所有非致命錯誤爲異常(exceptions):

Yii 缺省出錯頁面已經足夠好。不過你當然可以定製化這個出錯頁面來滿足項目需求。

最簡單的方法是創建一個定製錯誤頁面。首先你需要在應用程序config中配置 errorHandler 組件:

複製代碼
// ...
'components' => [
    // ...
    'errorHandler' => [
        'errorAction' => 'site/error',
    ],
]
複製代碼

 

這樣當錯誤發生時,Yii 將執行 site-controller 的 error-action 方法。該方法首先檢查是否有異常發生,如有則傳遞異常並繪製出錯頁面:

複製代碼
public function actionError()
{
    $exception = \Yii::$app->errorHandler->exception;
    if ($exception !== null) {
        return $this->render('error', ['exception' => $exception]);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章