1、Request組件
Request封裝了$_SERVER,統(tǒng)一了不同Web服務(wù)器的變量 , 并且提供$_POST,$_GET,$_COOKIES ,還包括HTTP中PUT、DELETE等方法
2、調(diào)用Request組件
1) 直接調(diào)用Request類 : \Yii::$app->request
2) 常用Request方法及屬性
判斷是不是Ajax請求:\Yii::$app->request->isAjax 判斷是不是Post請求:\Yii::$app->request->isPost 獲取用戶瀏覽器:\Yii::$app->request->userAgent 獲取用戶IP:\Yii::$app->request->userIp 讀取$_GET全部數(shù)據(jù) : \Yii::$app->request->get() 讀取$_GET數(shù)據(jù) :\Yii::$app->request->get(‘username’) 讀取$_POST全部數(shù)據(jù):\Yii::$app->request->post() 讀取$_POST數(shù)據(jù):\Yii::$app->request->post('username')更多的信息可以查閱:http://www.yiiframework.com/doc-2.0/yii-web-request.html
3、Html組件
\yii\helpers\Html組件 , 提供了封裝好的Html代碼 , 直接調(diào)用Html的相對方法就可以生成相對應(yīng)的Html代碼 。
4、調(diào)用常用的Html組件之直接生成Html
1) 生成表單
//beginForm(‘提交的Url’ , ‘類型POST或GET’ , ‘單表的屬性id,class等’) <?=\yii\helpers\Html::beginForm('' , 'post' , ['id' => 'addForm']);?> //endForm也是必須,就是閉合表單 <?=\yii\helpers\Html::endForm();?>2) 生成Text輸入框
//input(“類型text/password/text” , ‘name的名稱’ , ‘默認(rèn)值’ , ‘屬性例如class,id等’); <?=\yii\helpers\Html::input('text' , ‘name’, '' , ['class'=>'name' , 'id' => 'name']);?> //直接生成指定類型的typeInput(‘name的名稱’ , ‘默認(rèn)值’ , ‘屬性例如class,id等’) //直接生成password類型的輸入框 <?=\yii\helpers\Html::passwordInput('pwd' , '' , ['id' => 'password'])?> //直接生成text類型的輸入框 <?=\yii\helpers\Html::textInput("name" , '' , ['id' => 'name'])?> //直接生成hidden類型的隱藏框 <?=\yii\helpers\Html::hiddenInput("hidden" , '' , ['id' => 'hidden'])?>3) 生成textArea
//textarea(‘name的名稱’ , ‘默認(rèn)值’ , ‘屬性例如class,id等’) <?=\yii\helpers\Html::textarea('intro' , '' , ['class'=>'intro' ]);?>4) 生成radio 及radio列表
//radio(‘name的名稱’ , ‘是否選中true/false’ , ‘屬性例如class,id等’) <?=\yii\helpers\Html::radio('status' , false , ['class' =>'status'])?> //radioList(‘name的名稱’ ,’選中的值’ , ‘?dāng)?shù)組選中鍵值’ , ‘屬性例如class,id等’) <?=\yii\helpers\Html::radioList('fav' , 1 , [1 => 'test' , 2 => 'mrs' ] , ['class'=>'fav-list'])?>5) 生成checkbox 及checkbox列表
//checkbox(‘name的名稱’ , ‘是否選中true/false’ , ‘屬性例如class,id等’) <?=\yii\helpers\Html::checkbox("ckbox" , false , ['class' => 'ckbox'])?> //checkboxList(‘name的名稱’ ,’選中的值’ , ‘?dāng)?shù)組選中鍵值’ , ‘屬性例如class,id等’) <?=\yii\helpers\Html::checkboxList('cklist' , 1 , [1 => 'mr' , 2 => 's' ] , ['class'=>'cklist'])?>6)生成select下拉框
//dropDownList(‘name的名稱’ ,’選中的值’ , ‘?dāng)?shù)組選中鍵值’ , ‘屬性例如class,id等’) <?=\yii\helpers\Html::dropDownList('sts' , 0 , [1 => '是' , 0 => '否' ] , ['class'=>'sts'])?>7)生成label
//label(‘顯示的名稱’ , ‘for的字段’ , ‘屬性例如class,id等’) <?=\yii\helpers\Html::label('name:' , 'uname' , ['class'=>'label'])?>8)生成上傳控件
//fileInput(‘name的名稱’ , ‘默認(rèn)值’ , ‘屬性例如class,id等’) <?=\yii\helpers\Html::fileInput('image' , null , ['class'=>'upload' ])?>9)生成按鈕
//button(‘顯示的文字’ , ‘屬性例如class,id等’) <?=\yii\helpers\Html::button("按鈕" , ['class' => 'btn'])?> <?=\yii\helpers\Html::submitButton("提交按鈕" , ['class' => 'btn-submt'])?> <?=\yii\helpers\Html::resetButton("重置" , ['class' => 'btn-reset'])?>5、調(diào)用常用的Html組件之生成與Model字段關(guān)聯(lián)的Html
1)生成Text輸入框
//$model是實(shí)例化一個Model , title則是他的字段 //activeInput(‘input的類型text/password’ , ‘實(shí)例化Model’, ‘字段’ , ‘屬性例如class,id等’) <?=\yii\helpers\Html::activeInput('title' , $model, 'title' , ['class' => ‘input'])?> //直接生成指定類型的typeInput(‘ ‘實(shí)例化Model’ , ‘字段’ , ‘屬性例如class,id等’) <?=\yii\helpers\Html::activeTextInput($model, 'name' , ['class' => 'input'])?> <?=\yii\helpers\Html::activePasswordInput($model, 'pwd' , ['class' => 'input'])?> <?=\yii\helpers\Html::activeHiddenInput($model, 'name' , ['class' => 'input'])?>2)生成textArea
//activeTextarea(‘實(shí)例化Model’, ‘字段’ , ‘屬性例如class,id等’) <?=\yii\helpers\Html::activeTextarea($model , 'content' , ['class'=>'intro' ]);?>3)生成radio 及radio列表
//activeRadio(‘實(shí)例化Model’, ‘字段’ , ‘屬性例如class,id等’) <?=\yii\helpers\Html::activeRadio($model , 'sts', ['class' =>'status'])?> //activeRadioList(‘實(shí)例化Model’, ‘字段’, ‘?dāng)?shù)組(鍵值)’ , ‘屬性例如class,id等’) <?=\yii\helpers\Html::activeRadioList($model,'sts' , [1=>'mrs' ,2=>'s' ] , ['class'=>'st'])?>4)生成checkbox 及checkbox列表
//activeCheckbox(‘實(shí)例化Model’, ‘字段’ , ‘屬性例如class,id等’) <?=\yii\helpers\Html::activeCheckbox($model , 'sts' , ['class' => 'ckbox'])?> //activeCheckboxList(‘實(shí)例化Model’, ‘字段’, ‘?dāng)?shù)組(鍵值)’ , ‘屬性例如class,id等’) <?=\yii\helpers\Html::activeCheckboxList($model , 'sts',[1=>'f',2=>'m'], ['class'=>'ss'])?>5)生成select下拉框
//activeDropDownList(‘實(shí)例化Model’, ‘字段’, ‘?dāng)?shù)組(鍵值)’ , ‘屬性例如class,id等’) <?=\yii\helpers\Html::activeDropDownList($model,'fg',[1=>'f',2=>'m'] , ['class'=>'sx'])?>6、Html轉(zhuǎn)義和反轉(zhuǎn)義html代碼
<?php $html = ‘<b>test</b>’; //轉(zhuǎn)義html代碼 $thtml = \yii\helpers\Html::encode($html); //反轉(zhuǎn)義html代碼 $html = \yii\helpers\Html::decode($thtml); ?>更多關(guān)于Html 可以查閱: http://www.yiiframework.com/doc-2.0/yii-helpers-html.html
掃碼二維碼 獲取免費(fèi)視頻學(xué)習(xí)資料
- 本文固定鏈接: http://www.wangchenghua.com/post/5272/
- 轉(zhuǎn)載請注明:轉(zhuǎn)載必須在正文中標(biāo)注并保留原文鏈接
- 掃碼: 掃上方二維碼獲取免費(fèi)視頻資料
查 看2022高級編程視頻教程免費(fèi)獲取