1、打开文章控制器(PostsController)
在文章控制器中添加文章创建方法actionCreate。
#在文章控制器中添加actionCreate方法
/**
* 文章创建
*/
public function actionCreate()
{
$model = new PostsForm();
return $this -> render('create',['model' => $model]);
}然后在frontend/views/posts下创建create.php渲染页面
<?php
$this->title = '创建';
$this->params['breadcrumbs'][] = ['label' =>'文章','url'=>['posts/index']]; #面包屑 对于数据的封装
$this->params['breadcrumbs'][] = $this->title;
use yiibootstrapActiveForm;
use yiihelpersHtml;
?>
<div class="row">
<div class="col-lg-9">
<div class="panel-title box-title">
<span>创建文章</span>
</div>
<div class="panel-body">
<?php $form = ActiveForm::begin() ?>
<?= $form->field($model,'title')->textinput(['maxlength'=>true]) ?>
<?= $form->field($model,'cat_id')->textInput(['maxlength'=>true]) ?>
<?= $form->field($model,'label_img')->textInput(['maxlength'=>true]) ?>
<?= $form->field($model,'content')->textInput(['maxlength'=>true]) ?>
<?= $form->field($model,'tags')->textInput(['maxlength'=>true]) ?>
<div class="form-group">
<?= Html::submitButton('发布',['class'=>'btn btn-success'])?>
</div>
<?php ActiveForm::end() ?>
</div>
</div>
<div class="col-lg-3"></div>
</div>创建文章页面效果图

文章创建功能基本实现了,不过从截图中可以看出,这跟我们印象中的不太一样,确实,这里的创建文章功能确实太单调了,没关系,在后面的分享的教程中会逐步完善文章创建功能。
如果你的创建文章下面的属性是英文,那是因为你的文章表单模型中没有attributeLabels方法,只需加上就可以了
//语言包映射
public function attributeLabels()
{
return [
'id' => Yii::t('common','id'),
'title' => Yii::t('common', 'title'),
'content' => Yii::t('common', 'content'),
'label_img' => Yii::t('common', 'label_img'),
'cat_id' => Yii::t('common', 'cat_id'),
'tags' => Yii::t('common', 'tags'),
];
}actionCreate 添加代码截图:

attributeLabels 添加代码截图:

