1、创建留言信息数据表
create table feeds( id int(11) not null auto_increment, user_id int(11) not null comment '用户ID', content varchar(255) not null comment '留言内容', created_at int(11) not null comment '留言时间', primary key(id) )ENGINE=MyISAM AUTO_INCREMENT=19 DEFAULT CHARSET=utf8 COMMENT='留言信息表'
2、gii生成feedsModel,记得修改基础基础类为BaseModel

3、创建feedsForm表单模型
<?php
namespace frontend\models;
/**
* 留言表单模型
*/
use yii\base\Model;
class FeedsForm extends Model
{
public $content;
public $_lastError;
public function rules()
{
return [
['content', 'required'],
['content', 'string', 'max'=>255],
];
}
public function attributeLabels()
{
return [
'id' => 'ID',
'content' => '留言内容',
];
}
}
4、自定义留言板组件获取留言内容列表
①创建留言组件目录

②实现自定义留言组件类获取留言内容列表
<?php
namespace frontend\widgets\chat;
/**
* 留言板组件
*/
use frontend\models\FeedsForm;
use Yii;
use yii\bootstrap\Widget;
class ChatWidget extends Widget
{
public function run()
{
$feed=new FeedsForm();
$data['feed']=$feed->getList();
return $this->render('index',['data'=>$data]);
}
}
③在feedsForm表单模型中实现getLists方法获取留言内容列表
/**
* 获取留言列表
* @return array
*/
public function getList()
{
$model=new FeedsModel();
$res=$model->find()
->limit(10)
->with('user')
->orderBy(['id'=>SORT_DESC])
->asArray()
->all();
return $res?:[];
}
④组件留言板列表数据渲染
<?php
use yii\helpers\Url;
?>
<div class="pen"
<!-- 只言片语 -->
<div class="panel-title box-title">
<span><strong>只言片语</strong></span>
<span class="pull-right"><a href="#" class="font-12">更多》</a></span>
</div>
<div class="panel-body">
<form action="/" id="w0" method="post">
//隐藏域csrf验证不能少,post提交验证需要用到
<input name="_csrf-frontend" type="hidden" id="_csrf" value="<?= Yii::$app->request->csrfToken ?>">
<div class="form-group input-group field-feed-content required">
<textarea id="feed-content" class="form-control" name="content" placeholder="我的留言" rows="" cols=""></textarea>
<span class="input-group-btn">
<button type="button" data-url="<?=Url::to(['site/add-feed'])?>" class="btn btn-primary j-feed" style="width: auto;height:52px;margin-top:-1px">发表</button>
</span>
</div>
</form>
<?php if (!empty($data['feed'])):?>
<ul class="media-list media-feed feed-index ps-container ps-active-y">
<?php foreach ($data['feed'] as $list):?>
<li class="media">
<div class="media-left"><a href="#" rel="author" data-original-title="" title="">
<img alt="" class="avatar-img" style="width:37px;height:37px;" src="/statics/images/tx/1.jpg"/></a></div>
<div class="media-body">
<div class="media-content">
<a href="#" ><?=$list['user']['username']?>: </a>
<span><?=$list['content']?></span>
</div>
<div class="media-action">
<?=date('Y-m-d h:i:s',$list['created_at'])?>
</div>
</div>
</li>
<?php endforeach;?>
</ul>
<?php endif;?>
</div>
⑤首页调用留言板列表组件
<?= ChatWidget::widget()?>

5、留言添加功能的实现
通过异步加载的方法实现留言组件面板中添加留言
①在SiteController中添加actionAddFeed方法
/**
* 留言添加
*/
public function actionAddFeed()
{
$model=new FeedsForm();
$model->content=Yii::$app->request->post('content');
if ($model->validate()){
if ($model->create()){
return json_encode(['status'=>true]);
}
}
return json_encode(['status'=>false,'msg'=>'留言发布失败']);
}
②feedsForm表单中实现create方法保存留言
/**
* 保存留言
*/
public function create()
{
try{
$model=new FeedsModel();
$model->user_id=\Yii::$app->user->identity->id;
$model->content=$this->content;
$model->created_at=time();
if (!$model->save())
throw new \Exception('留言保存失败');
return true;
}catch (\Exception $e){
$this->_lastError=$e->getMessage();
return false;
}
}
③添加异步请求js实现异步添加保存留言
js文件


④配置文件引入site.js
<?php
namespace frontend\assets;
use yii\web\AssetBundle;
/**
* Main frontend application asset bundle.
*/
class AppAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
'statics/css/site.css',
'statics/font-awesome/css/font-awesome.min.css',
];
public $js = [
'statics/js/site.js',
//如果发布文章的时候tag标签输入框不见了,把下面这个jquery包引用注释掉
//'statics/js/jquery-3.3.1.min.js',
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
6、登录用户ID获取
用户登录后才能添加留言
在feedsModel中通过关联关系获取登录用户id
public function getUser()
{
return $this->hasOne(UserModel::className(), ['id'=>'user_id']);
}
留言板效果:

在实际操作过程中有什么问题可以留言,获取通过首页联系方式咨询。
