近期Yii2学习用法总结 [ 2.0 版本 ]
1、一些常用的参数总结:
系统类型: <?= php_uname() ?>
解译引擎: <?= $_SERVER['SERVER_SOFTWARE'] ?>,
Zend: <?= Zend_Version() ?>
数据库: MySql:<?= (new yii\db\Query())->select('VERSION()')->one()['VERSION()'] ?>
服务器
服务器IP:<?= GetHostByName($_SERVER['SERVER_NAME']) ?>
程序目录:<?= Yii::$app->BasePath ?>
2、Yii2设定场景规则的形式
public function scenarios()
{
return [
'create' => ['username', 'email', 'password', 'status', 'role','sex','citys','provinces','countrys','mobile_phone'],
'update' => ['username', 'email', 'status', 'role','sex','citys','provinces','countrys','mobile_phone'],
'delete' => ['status'],
'signup' => ['username', 'email', 'password', 'role'],
];
}
另外还有一种:
$storeUser->save(false);
这个可以把所有的规则去掉,只留设定的规则
3、YIi2原生与非原生的对比
Html::a('取消订单','javascript:;',['url'=>Url::to(['benefit-to-pro/min-product','base_id'=>$model->base_id]),'class'=>'openmodal'])
<a href=javascript:; src=\aiyidd\backend\web\index.php?r=benefit-to-pro/min-product&base_id=$model->base_id class=openmodal>追踪查看价格</a>
4、自定义表单样式
<?php $form = ActiveForm::begin([
'options' => ['class' => 'form-horizontal'],
'fieldConfig' => [
'options'=>['class'=>'control-group'],
'template' => "{label}<div class=\"controls\">{input}<span class=\"help-inline\">{error}</span></div>",
'inputOptions'=>['class'=>'input-xlarge'],
'labelOptions' => ['class' => 'control-label'],
],
]); ?>
<?= $form->field($model, 'title')->textInput(['maxlength' => 255]) ?>
<?= $form->field($model, 'pic')->widget('pjkui\kindeditor\KindEditor',['editorType'=>'image-dialog','callbackjs'=>'$("#brand").attr("src",url);']) ?>
<img src="<?php echo $model->pic ?>" width="540" height="225" style='width:540px;height:225px' id="brand">
<?= $form->field($model, 'status')->radioList(['1'=>'正常','0'=>'不可用','2'=>'发布中']) ?>
<?= Html::submitButton($model->isNewRecord ? '创建' : '修改', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
<?php ActiveForm::end(); ?>
5、自定义分页样式
<?= GridView::widget([
'dataProvider' => $dataProvider,
//'filterModel' => $searchModel,
'layout'=>'<div id="DataTables_Table_0_wrapper" class="dataTables_wrapper">
{items}
<div class="row-fluid">
<div class="span12"><div class="dataTables_info" id="DataTables_Table_0_info">{summary}</div></div>
<div class="span12 center">
<div class="dataTables_paginate paging_bootstrap pagination">
{pager}
</div>
</div>
</div>
</div>
<div class="row">
',
'pager'=>[
'prevPageLabel'=>'← 上一页',
'nextPageLabel'=>'下一页 →',
],
'columns' => [
['class' => 'yii\grid\SerialColumn'],
['class' => 'yii\grid\CheckboxColumn',
'checkboxOptions' => function($model, $key, $index, $column) {
return ['value' => $model->id];
}],
['attribute'=>'pics','value'=>function($model){
$path = '/static/images/brand-head.jpg';
if(!empty($model->pics_arr)){
$path = $model->pics_arr[0]['path'];
}
return "<img src='$path' width=75 height=75>";
},'format'=>'html'],
'name',
['label'=>'品牌', 'attribute' => 'name_zh', 'value' => 'brand.name_zh'],
['label'=>'类目', 'attribute' => 'catename', 'value' => 'category.name'],
[
'attribute' => 'status',
'label' => '状态',
'value'=>function($model){
return $model->status_text;
},
'filter' => \common\models\ProductBase::get_status(),
],
['attribute'=>'storage_time','format'=>['date', 'php:Y-m-d']],
['label'=>'更多操作', 'format' => 'raw', 'value' => function($model){
return "<a class='btn btn-success' href='".Url::to(['product-base/view','id'=>$model->id])."' alt='查看'>
<i class='icon-zoom-in'></i>
</a>
<a class='btn btn-info' href='".Url::to(['product-base/update','id'=>$model->id])."' alt='修改'>
<i class='icon-edit'></i>
</a>
<a class='btn btn-danger' href='".Url::to(['product-base/delete','id'=>$model->id])."' alt='删除'>
<i class='icon-trash'></i>
</a>
";
}],
[
'label' => '更多操作',
'format'=>'raw',
'value' => function($model){
return Html::a('店铺商品', ['product/index','id' => $model->id], ['title' => '店铺商品','class'=>'btn btn-info btn-setting']);
}
],
],
]); ?>
5、Yii2查询的用法
$request = Yii::$app->request;
$shopid = $request->post('shopid');
$gps = $request->post('gps');
$connection = Yii::$app->db;
$sqlb = "select s.* from store_show s where s.type=0 and s.store_id='" . $shopid . "' ";
$rowsb = $connection->createCommand($sqlb)->query();
tiandi2050
注册时间:2015-05-25
最后登录:2017-03-28
在线时长:16小时6分
最后登录:2017-03-28
在线时长:16小时6分
- 粉丝10
- 金钱235
- 威望60
- 积分995
共 3 条评论
请问导航条左边怎样能多加几个吗?目前只能有一个“首页”
可以 是不是在layouts布局文件中
@tiandi2050 嗯,这个知道,在layouts文件夹下的main.php文件里面,但是默认只有一个首页,我加别的进去都不行
<?php NavBar::begin([ 'brandLabel' => '大海', 'brandUrl' => Yii::$app->homeUrl, 'options' => [ 'class' => 'navbar-inverse navbar-fixed-top', ], ]); $menuItems = [ ['label' => '首页', 'url' => ['/site/index']], ['label' => '品牌管理','url' => ['/brand/index']], ['label' => '品牌惠','url' => ['/brand-benefit/index']], ['label' => '角色管理','url' => ['/role/index']], ['label' => '权限管理','url' => ['/permission/index']], ['label' => '分类管理','url' => ['/category/index']], ['label' => '文章管理','url' => ['/news/index']], ['label' => '广告管理','url' => ['/adposition/index']], ['label' => '商铺管理','url' => ['/store/index']], ['label' => '品牌商品','url' => ['/product-base/index']], ['label' => '用户', 'url' => ['/user/index']], ]; if (Yii::$app->user->isGuest) { $menuItems[] = ['label' => '登录', 'url' => ['/site/login']]; } else { $menuItems[] = [ 'label' => '退出 (' . Yii::$app->user->identity->username . ')', 'url' => ['/site/logout'], 'linkOptions' => ['data-method' => 'post'] ]; } echo Nav::widget([ 'options' => ['class' => 'navbar-nav navbar-right'], 'items' => $menuItems, ]); NavBar::end(); ?>
谢谢,不过那个增加的都在右边是吧,除了那个“大海”,我的意思是在大海前面增加一个
@tiandi2050
@belone echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'], 'items' => $menuItems, ]);
把navbar-nav navbar-right 改成 navar-nav nav-left就可以了
总结的很好 很有用
GridView的分页能添加首页和末页么,现在只能是数字显示分页