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

<?php

return [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=;dbname',
    'username' => 'root',
    'password' => '',
    'charset' => 'utf8',
];?>

 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]); } }

YIi2參考文檔 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM