琯琯博客 纸上得来终觉浅,绝知此事要躬行。

Yii2 学习笔记之助手类

琯琯
2017-03-11
PHP  Yii
 

一、数组助手类

<?php
// 常用的就是建立哈希表,map()方法。一般在使用dropDownList的时候,
// 会从查询出来的对象列表中获取到这样的$array供其使用。
// 参考http://www.yiichina.com/doc/guide/2.0/helper-array
$array = [
    ['id' => '123', 'name' => 'aaa', 'class' => 'x'],
    ['id' => '124', 'name' => 'bbb', 'class' => 'x'],
    ['id' => '345', 'name' => 'ccc', 'class' => 'y'],
];
ArrayHelper::map($array, 'id', 'name');
// 结果是:
[
    '123' => 'aaa',
    '124' => 'bbb',
    '345' => 'ccc',
]

二、HTML 助手类

如果你知道 input 类型,更方便的做法是使用以下快捷方法:

<?php
yii\helpers\Html::buttonInput()
yii\helpers\Html::submitInput()
yii\helpers\Html::resetInput()
yii\helpers\Html::textInput()
yii\helpers\Html::activeTextInput()
yii\helpers\Html::hiddenInput()
yii\helpers\Html::activeHiddenInput()
yii\helpers\Html::passwordInput()
yii\helpers\Html::activePasswordInput()
yii\helpers\Html::fileInput()
yii\helpers\Html::activeFileInput()
yii\helpers\Html::textarea()
yii\helpers\Html::activeTextarea()
<?php
// Radios 和 checkboxes 在方法的声明上有一点点不同:
// http://www.yiichina.com/doc/guide/2.0/helper-html
<?= Html::radio('agree', true, ['label' => 'I agree']) ?>
<?= Html::activeRadio($model, 'agree', ['class' => 'agreement']) ?>

<?= Html::checkbox('agree', true, ['label' => 'I agree']) ?>
<?= Html::activeCheckbox($model, 'agree', ['class' => 'agreement']) ?>

// Dropdown list 和 list box 将会如下渲染:
<?= Html::dropDownList('list', $currentUserId, ArrayHelper::map($userModels, 'id', 'name')) ?>
<?= Html::activeDropDownList($users, 'id', ArrayHelper::map($userModels, 'id', 'name')) ?>

<?= Html::listBox('list', $currentUserId, ArrayHelper::map($userModels, 'id', 'name')) ?>
<?= Html::activeListBox($users, 'id', ArrayHelper::map($userModels, 'id', 'name')) ?>

三、HTML Activeform表单部件

<?php
textInput();         // 文本框
passwordInput();     // 密码框
radio(),radioList(); // 单选框
checkbox()           // 复选框 1
checkboxList();      // 复选框 2
dropDownList();      // 下拉框
hiddenInput();       // 隐藏域
textarea(['rows'=3]); // 文本域
fileInput();         // 文件上传
submitButton();      // 提交按钮
resetButtun();       // 重置按钮
<?php
<?= $form = ActiveForm::begin(['action' => ['test/getpost'], 'method'=>'post',]) ?>

<?= $form->field($model, 'username')->textInput(['maxlength' => 20]) ?>

<?= $form->field($model, 'create_at')->widget(DatePicker::className(), ['clientOptions' => ['dateFormat' => 'yy-mm-dd']])->textInput(['placeholder' => '创建时间']) ?>

<?= $form->field($model, 'password')->passwordInput(['maxlength' => 20]) ?>

<?= $form->field($model, 'sex')->radioList(['1'=>'男', '0'=>'女']) ?>

<?= $form->field($model, 'edu')->dropDownList(['1'=>'大学', '2'=>'高中', '3'=>'初中'], ['prompt'=>'请选择', 'style'=>'width:120px']) ?>

<?= $form->field($model, 'file')->fileInput() ?>

<?= $form->field($model, 'hobby')->checkboxList(['0'=>'篮球', '1'=>'足球', '2'=>'羽毛球', '3'=>'乒乓球']) ?>

<?= $form->field($model, 'info')->textarea(['rows'=>3]) ?>

<?= $form->field($model, 'userid')->hiddenInput(['value'=>3]) ?>

<?= Html::submitButton('提交', ['class'=>'btn btn-primary', 'name' =>'submit-button']) ?>

<?= Html::resetButton('重置', ['class'=>'btn btn-primary', 'name' =>'submit-button']) ?>

<?= ActiveForm::end() ?>

四、URL助手类

<?php
// 参考http://www.yiichina.com/doc/guide/2.0/helper-url
// 返回首页
$relativeHomeUrl = Url::home();

// /index.php?r=site/index
echo Url::to(['site/index']);

// /index.php?r=site/index&src=ref1#name
echo Url::to(['site/index', 'src' => 'ref1', '#' => 'name']);

// /index.php?r=post/edit&id=100     assume the alias "@postEdit" is defined as "post/edit"
echo Url::to(['@postEdit', 'id' => 100]);

// the currently requested URL
echo Url::to();

// /images/logo.gif
echo Url::to('@web/images/logo.gif');

// images/logo.gif
echo Url::to('images/logo.gif');

// http://www.example.com/images/logo.gif
echo Url::to('@web/images/logo.gif', true);

// https://www.example.com/images/logo.gif
echo Url::to('@web/images/logo.gif', 'https');

五、面包屑小部件

<?php
// 摘自view文件的代码
$this->params['breadcrumbs'][] = ['label' => '文章管理', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;

// 摘自layout文件的代码
echo Breadcrumbs::widget([
    'tag'=>'ol',
    'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],
]);

(完)


相关文章

评论

内容导航