PHP框架-yii2(一)

  1. yii2的常規安裝,使用composer很簡單;
  2. 與以前對yii1的瞭解,剛接觸的yii2變化之一是命名空間的增長,還有就是有些代碼的格式有些變化;

 

細節筆記:php

  1. yii2開發版本基礎程序是有註冊登錄功能的,字段驗證方面較yii1有所進步,能夠直接驗證某些字段在數據庫中是否存在,驗證規則以下:
 1 public function rules()
 2     {
 3         return [
 4             ['username', 'filter', 'filter' => 'trim'],
 5             ['username', 'required'],
 6             ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],//直接判斷用戶名在數據庫中是否存在
 7             ['username', 'string', 'min' => 2, 'max' => 255],
 8 
 9             ['email', 'filter', 'filter' => 'trim'],
10             ['email', 'required'],
11             ['email', 'email'],
12             ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],//直接判斷郵箱在數據庫中是否存在
13 
14             ['password', 'required'],
15             ['password', 'string', 'min' => 6],
16         ];
17     }

  2. 表單生成,格式有所變化。代碼以下:ajax

 1 <div class="user-register">
 2     <?php $form = ActiveForm::begin([
 3         'id' => 'form-register',
 4         'enableAjaxValidation'=>true,//是否使用ajax驗證
 5         'validateOnSubmit'=>false]//yii1時雖然使用ajax驗證但提交時則不是ajax驗證,yii2提交時還是ajax驗證,開啓此項表明提交不驗證
 6     ); ?>
 7         <?= $form->field($model, 'user_login')->textInput() ?>
 8         <?= $form->field($model, 'user_pass')->passwordInput() ?>
 9         <?= $form->field($model, 'user_nicename') ?>
10         <?= $form->field($model, 'user_email') ?>
11         <?= $form->field($model, 'verifyCode')->widget(Captcha::className(), ['template' => '<div class="row"><div class="col-lg-2">{input}</div><div class="col-lg-2">{image}</div></div>',]) ?>
12         <div class="form-group">
13             <?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
14         </div>
15     <?php ActiveForm::end(); ?>
16 </div><!-- user-register -->

  3.yii2的身份驗證使用了新的機制,可是沒搞懂怎麼弄得,我想覆蓋都不知道怎麼覆蓋,密碼的加密方式也沒搞懂,數據庫

  4.yii2帶了郵箱類,可在main.php的components項中添加配置,代碼以下:swift

//配置代碼
'mailer' => [ 'class' => 'yii\swiftmailer\Mailer', 'transport' => [ 'class' => 'Swift_SmtpTransport', 'host' => 'smtp.163.com', 'username' => '15618380091@163.com', 'password' => '*******', 'port' => '25', 'encryption' => 'tls', ], 'messageConfig'=>[ 'charset'=>'UTF-8', 'from'=>['15618380091@163.com'=>'admin'] ], ],

//controller控制器中 代碼: <?php $mail= Yii::$app->mailer->compose();

$mail->setTo('1401619705@qq.com');
$mail->setSubject("郵件測試"); $mail->setTextBody('zheshisha '); $mail->setHtmlBody("問我我我我我");
if($mail->send()) echo "success"; else echo "failse"; die();
?>

  5.yii2中mail的配置useFileTransport爲true,只生成郵件,並未發送,應該爲false,另外配置文件params.php中supportEmail應設置爲郵箱用戶名,設置正確的話,重置密碼應該就能用了;yii2

相關文章
相關標籤/搜索