yii2 0 Html助手和Request元件

2021-08-04 15:34:37 字數 4047 閱讀 8821

<?php 

//引入命名空間

useyii\helpers\html;

?>

<?php 

//【一】表單:html::beginform(提交位址,提交方法,屬性陣列);?>

=html::beginform('','post',['id'=>'form','class'=>'form','data'=>'myself']);?>

<?php

//【二】輸入框:html::input(型別,name值,預設值,屬性陣列);?>

=html::input('text','test','',['class' => 'form-control','placeholder'=>'hehe']);?>

=html::input('email','email','[email protected]',['class' => 'form-control']);?>

=html::input('password','pwd','',['class' => 'form-control']);?>

=html::input('hidden','hidden','',['class' => 'form-control']);?>

<?php

//html::表單型別input(name值,預設值,屬性陣列);?>

=html::textinput('test','hehe',['class' => 'form-control']);?>

=html::textinput('email','[email protected]',['class' => 'form-control']);?>

=html::passwordinput('pwd','',['class' => 'form-control']);?>

=html::hiddeninput('hidden','',['class' => 'form-control']);?>

<?php

//【三】文字域:html::textarea(表單name,預設值,屬性陣列);?>

=html::textarea('area','',['class'=>'form-control','rows'=>'3']);?>

<?php

//【四】單選按鈕:html::radio(name值,是否選中,屬性陣列);?>

=html::radio('***',true,['calss'=>'form-control']);?>

<?php

//單選按鈕列表:html:;radiolist(name值,選中的值,鍵值對列表,屬性陣列); ?>

=html::radiolist('height','1',['1'=>'160','2'=>'170','3'=>'180'],['class'=>'form-control']);?>

<?php

//【五】核取方塊:html::checkbox(name值,是否選中,屬性陣列);?>

=html::checkbox('haha',true,['calss'=>'form-control']);?>

<?php

//單選按鈕列表:html:;checkboxlist(name值,選中的值,鍵值對列表,屬性陣列); ?>

=html::checkboxlist('xixi','1',['1'=>'160','2'=>'170','3'=>'180'],['class'=>'form-control']);?>

<?php

//【六】下拉列表:html:;dropdownlist(name值,選中的值,鍵值對列表,屬性陣列); ?>

=html::dropdownlist('list','2',['1'=>'160','2'=>'170','3'=>'180'],['class'=>'form-control']);?>

<?php

//【七】控制標籤label:html::label(顯示內容,for值,屬性陣列); ?>

=html::label('顯示的','test',['style'=>'color:#ff0000']);?>

<?php

//【八】上傳控制項:html::fileinput(name值,預設值,屬性陣列); ?>

=html::fileinput('img',null,['class'=>'btn btn-default']);?>

<?php

//【九】按鈕:; ?>

=html::button('普通按鈕',['class'=>'btn btn-primary']);?>

=html::submitbutton('提交按鈕',['class'=>'btn btn-primary']);?>

=html::resetbutton('重置按鈕',['class'=>'btn btn-primary']);?>

=html::endform();?>

在test控制器中注入檢視:

return $this->render('index');

瀏覽結果:

2 .與模型字段關聯的html

和生成普通的表單基本一樣,只是需要在操作中對檢視注入模型,檢視中表單生成方式前面多了active。

在test控制器的index操作中注入檢視與模型資料:

<?php 

namespace

useyii\web\controller; //引入命名空間

useclass

testcontroller

extends

controller

}

<?php 

useyii\helpers\html;

?>

=html::beginform('','post',['name'=>'article']);?>

=html::activeinput('text',$model,'title',['class'=>'form-control']);?>

=html::activetextinput($model,'title',['class'=>'form-control']);?>

=html::activetextarea($model,'desc',['class' => 'form-control']);?>

=html::activetextarea($model,'content',['class'=>'form-control']);?>

=html::endform();?>

我們建立乙個按鈕來提交看一下在test控制器的index操作中判斷提交列印一下看是否接受到引數,在$model = article::findone(1);上面一行新增**:

header('content-type:text/html;charset=utf-8');

echo '

';

echo '

';

exit();}

得到提交的資料了:

3 .html轉義

yii2提供了html轉義與反轉義,使用\yii\helpers\html::encode($html)和\yii\helpers\html::decode($html)

4 .可以通過\yii\helpers\html::error(模型,欄位名,屬性陣列)來進行驗證等錯誤輸出.

5 .生成標籤的**類似:

= html::tag('p', html::encode($user->name), ['class' => 'username']) ?>

yii2 0奧秘窺探 助手函式

在中文手冊 助手函式 只提了三個函式,分別是url html array。具體檢視 一時好奇開啟yii原始碼的中vendor yiisoft yii2.0 helpers 中有更多手冊沒有尚未提到,好用的助手函式 1.列印類中 vardumper dump passways,10,true 第乙個引...

初識yii2 0和es介面

做的乙個專案,資料是從es獲取的,所以最近研究了下yii2.0和es.yii2.0有自身帶有乙個擴充套件 yii elasticsearch.php,在配置的時候 config web.php中配置 elasticsearch class yii elasticsearch connection i...

Yii2 0 redis的配置和使用

第二步 window在dos下進入解壓後的目錄,然後執行redis server.exe 出現啟動成功的視窗 第三步 出現以上介面的時候說明啟動成功,然後另開乙個dos視窗進入目錄下執行redis cli.exe啟動使用者端服務,執行命令 注 以上已經可以正常執行redis命令了,但是這個時候的第乙...