前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Yii2 学习笔记之助手类

Yii2 学习笔记之助手类

作者头像
guanguans
发布2018-05-09 16:22:32
8160
发布2018-05-09 16:22:32
举报
文章被收录于专栏:琯琯博客琯琯博客

一、数组助手类

代码语言:javascript
复制
<?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 类型,更方便的做法是使用以下快捷方法:

代码语言:javascript
复制
<?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()
代码语言:javascript
复制
<?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表单部件

代码语言:javascript
复制
<?php
textInput();         // 文本框
passwordInput();     // 密码框
radio(),radioList(); // 单选框
checkbox()           // 复选框 1
checkboxList();      // 复选框 2
dropDownList();      // 下拉框
hiddenInput();       // 隐藏域
textarea(['rows'=3]); // 文本域
fileInput();         // 文件上传
submitButton();      // 提交按钮
resetButtun();       // 重置按钮
代码语言:javascript
复制
<?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助手类

代码语言:javascript
复制
<?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');

五、面包屑小部件

代码语言:javascript
复制
<?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'] : [],
]);

(完)

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-03-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、数组助手类
  • 二、HTML 助手类
  • 三、HTML Activeform表单部件
  • 四、URL助手类
  • 五、面包屑小部件
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档