Yii2 girdView文本溢出显示…
[
‘attribute’=>’content’,
‘label’=>’内容’,
‘format’=>’raw’,
‘value’=>function($model){
return “
“.$model->content.”
“;
//
},
],
只查看数据,消除删除和编辑图标
1 2 3 4
| [ 'class'=>'yii\grid\ActionColumn', 'template'=>'{view}' ],
|
控制器向视图传递参数
控制器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public function actionIndex() { $view = Yii::$app->view; $date=(new Query())->from('visit_count')->select('date')->orderBy('date')->all(); foreach($date as $eachdate) { $result1[] = $eachdate['date']; } $view->params['data1'] = $result1; $count=(new Query())->from('visit_count')->select('nums')->orderBy('date')->all(); foreach($count as $eachcount) { $result2[] = $eachcount['nums']; } $view->params['data2'] = $result2;
return $this->render('index'); }
|
视图:(使用echarts)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <?php use Hisune\EchartsPHP\ECharts; $this->title = '访问量统计'; $chart = new ECharts(); $chart->tooltip->show = true; $chart->legend->data[] = '访问量'; $t = $this->params['data1']; $chart->xAxis[] = array( 'type' => 'category', 'data' => $t ); $chart->yAxis[] = array( 'type' => 'value' ); $k = $this->params['data2']; $chart->series[] = array( 'name' => '访问量', 'type' => 'line', 'data' => $k ); echo $chart->render('simple-custom-id'); ?>
|
管理员修改密码的功能
弹出消息框:
在view文件中加:
1
| Yii::$app->session->getFlash('error');
|
在model文件中加:
1
| Yii::$app->getSession()->setFlash('error','旧密码错误');
|
修改密码:
model文件:PasswordForm.php,主要用到validatePassword()函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| <?php namespace backend\models;
use Yii; use common\models\Adminuser; use yii\base\Model;
class PasswordForm extends Adminuser { public $oldpwd; public $newpwd;
public function rules() { return [ ]; } public function changePassword() { if (!$this->validate()) { return null; } $id = YII::$app->user->id; $admin= Adminuser::findIdentity($id); $password = $admin->password_hash; if(Yii::$app->getSecurity()->validatePassword($this->oldpwd, $password)){ $newpwd = Yii::$app->getSecurity()->generatePasswordHash($this->newpwd); $admin->password_hash = $newpwd;
if($admin->save()){ Yii::$app->getSession()->setFlash('success', '修改成功'); return true; }else{ return false; } } else{ Yii::$app->session->setFlash('error','旧密码错误'); return false; } } }
|
view文件:password.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <?php
use yii\helpers\Html; use yii\bootstrap\ActiveForm;
$this->title = '修改密码'; $this->params['breadcrumbs'][] = ['label' => 'Adminusers', 'url' => ['index']]; $this->params['breadcrumbs'][] = $this->title; Yii::$app->session->getFlash('error'); Yii::$app->session->getFlash('success'); ?> <div class="user-updatepwd"> <div class="row" > <div class="col-md-4 col-md-offset-4"> <div class="panel-heading"> </div> <div class="panel-body"> <!-- <div class="col-lg-5"> --> <?php $form = ActiveForm::begin(['id' => 'form-signup']); ?> <?= $form->field($model, 'oldpwd')->label('原密码:',['style'=>['float'=>'left','margin-top'=>'10px']])->passwordInput(['placeholder'=>'请输入原密码']) ?> <?= $form->field($model, 'newpwd')->label('新密码:',['style'=>['float'=>'left','margin-top'=>'10px']])->passwordInput(['placeholder'=>'请输入6~18位英文字母、数字']) ?> <div class="form-group"> <?= Html::submitButton('确认', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?> </div>
<?php ActiveForm::end(); ?> </div> </div> </div>
</div>
|
controller文件中添加函数actionPassword()
1 2 3 4 5 6 7 8 9 10
| public function actionPassword(){ $model=new PasswordForm(); $request = YII::$app->request;
if($request->isPost && $model->load(Yii::$app->request->post()) && $model->changePassword()){ return $this->redirect('index'); }else{ return $this->render('password',['model'=>$model]); } }
|
显示PDF
将pdf或含该pdf的文件夹(personal/作业1(1711436_皮春莹).pdf)放到web目录下,在视图文件中加按钮,url为:
1
| 'url' => ['/personal/作业1(1711436_皮春莹).pdf']
|
点进去之后就跳到pdf,然后点回退按钮可以回到原来的界面
下载压缩包
同上,url为:
1
| 'url' => ['/personal/作业2(1711436_皮春莹).rar']
|
点击后直接开始下载
PHP中显示日期的date()函数
显示年月日:date(‘Y-m-d’);
年月日时分秒:date(‘Y-m-d H:i:s’)