Skip to content

Commit 0c65e75

Browse files
committed
后台单页Page的CURD
1 parent 920debf commit 0c65e75

File tree

7 files changed

+529
-0
lines changed

7 files changed

+529
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
3+
namespace backend\controllers;
4+
5+
use Yii;
6+
use backend\models\Page;
7+
use backend\models\search\PageSearch;
8+
use yii\web\NotFoundHttpException;
9+
use yii\filters\VerbFilter;
10+
11+
/**
12+
* 后台单页文章控制器
13+
*/
14+
class PageController extends BaseController
15+
{
16+
/**
17+
* @inheritdoc
18+
*/
19+
public function behaviors()
20+
{
21+
return [
22+
'verbs' => [
23+
'class' => VerbFilter::className(),
24+
'actions' => [
25+
'delete' => ['POST'],
26+
],
27+
],
28+
];
29+
}
30+
31+
/**
32+
* 列表页
33+
* @return mixed
34+
*/
35+
public function actionIndex()
36+
{
37+
/* 添加当前位置到cookie供后续跳转调用 */
38+
$this->setForward();
39+
40+
$searchModel = new PageSearch();
41+
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
42+
43+
return $this->render('index', [
44+
'searchModel' => $searchModel,
45+
'dataProvider' => $dataProvider,
46+
]);
47+
}
48+
49+
/**
50+
* ---------------------------------------
51+
* 添加
52+
* @return mixed
53+
* ---------------------------------------
54+
*/
55+
public function actionAdd()
56+
{
57+
$model = $this->findModel(0);
58+
59+
if (Yii::$app->request->isPost) {
60+
$data = Yii::$app->request->post('Page');
61+
62+
/* 表单数据加载、验证、数据库操作 */
63+
if ($this->saveRow($model, $data)) {
64+
$this->success('操作成功', $this->getForward());
65+
}else{
66+
$this->error('操作错误');
67+
}
68+
}
69+
70+
/* 获取模型默认数据 */
71+
$model->loadDefaultValues();
72+
/* 渲染模板 */
73+
return $this->render('edit', [
74+
'model' => $model,
75+
]);
76+
}
77+
78+
/**
79+
* ---------------------------------------
80+
* 编辑
81+
* @param integer $id
82+
* @return mixed
83+
* ---------------------------------------
84+
*/
85+
public function actionEdit($id)
86+
{
87+
$model = $this->findModel($id);
88+
89+
if (Yii::$app->request->isPost) {
90+
$data = Yii::$app->request->post('Page');
91+
//$data['update_time'] = time(); // backend/models/Article->behaviors()自动完成时间更新
92+
/* 表单数据加载、验证、数据库操作 */
93+
if ($this->saveRow($model, $data)) {
94+
$this->success('操作成功', $this->getForward());
95+
}else{
96+
$this->error('操作错误');
97+
}
98+
}
99+
100+
/* 渲染模板 */
101+
return $this->render('edit', [
102+
'model' => $model,
103+
]);
104+
}
105+
106+
/**
107+
* ---------------------------------------
108+
* 删除或批量删除
109+
* ---------------------------------------
110+
*/
111+
public function actionDelete()
112+
{
113+
$model = $this->findModel(0);
114+
if($this->delRow($model, 'id')){
115+
$this->success('删除成功', $this->getForward());
116+
} else {
117+
$this->error('删除失败!');
118+
}
119+
}
120+
121+
/**
122+
* Finds the Page model based on its primary key value.
123+
* If the model is not found, a 404 HTTP exception will be thrown.
124+
* @param integer $id
125+
* @return Page the loaded model
126+
* @throws NotFoundHttpException if the model cannot be found
127+
*/
128+
protected function findModel($id)
129+
{
130+
if ($id == 0) {
131+
return new Page();
132+
}
133+
if (($model = Page::findOne($id)) !== null) {
134+
return $model;
135+
} else {
136+
throw new NotFoundHttpException('The requested page does not exist.');
137+
}
138+
}
139+
}

backend/models/Page.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace backend\models;
4+
5+
use Yii;
6+
7+
/*
8+
* ---------------------------------------
9+
* 文章模型
10+
* ---------------------------------------
11+
*/
12+
class Page extends \common\models\Page
13+
{
14+
/**
15+
* @inheritdoc
16+
*/
17+
public function behaviors()
18+
{
19+
return [
20+
/**
21+
* 写库和更新库时,时间自动完成
22+
* 注意rules验证必填时可使用AttributeBehavior行为,model的EVENT_BEFORE_VALIDATE事件
23+
*/
24+
'timestamp' => [
25+
'class' => 'yii\behaviors\TimestampBehavior',
26+
'createdAtAttribute' => 'create_time',
27+
'updatedAtAttribute' => 'update_time',
28+
'value' => time(),
29+
],
30+
];
31+
}
32+
33+
34+
35+
}

backend/models/search/PageSearch.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace backend\models\search;
4+
5+
use Yii;
6+
use yii\base\Model;
7+
use yii\data\ActiveDataProvider;
8+
use backend\models\Page;
9+
10+
/**
11+
* PageSearch represents the model behind the search form of `backend\models\Page`.
12+
*/
13+
class PageSearch extends Page
14+
{
15+
/**
16+
* @inheritdoc
17+
*/
18+
public function rules()
19+
{
20+
return [
21+
[['id', 'create_time', 'update_time', 'status'], 'integer'],
22+
[['name', 'title', 'content'], 'safe'],
23+
];
24+
}
25+
26+
/**
27+
* @inheritdoc
28+
*/
29+
public function scenarios()
30+
{
31+
// bypass scenarios() implementation in the parent class
32+
return Model::scenarios();
33+
}
34+
35+
/**
36+
* Creates data provider instance with search query applied
37+
*
38+
* @param array $params
39+
*
40+
* @return ActiveDataProvider
41+
*/
42+
public function search($params)
43+
{
44+
$query = Page::find();
45+
46+
// add conditions that should always apply here
47+
48+
$dataProvider = new ActiveDataProvider([
49+
'query' => $query,
50+
]);
51+
52+
$this->load($params);
53+
54+
if (!$this->validate()) {
55+
// uncomment the following line if you do not want to return any records when validation fails
56+
// $query->where('0=1');
57+
return $dataProvider;
58+
}
59+
60+
// grid filtering conditions
61+
$query->andFilterWhere([
62+
'id' => $this->id,
63+
'create_time' => $this->create_time,
64+
'update_time' => $this->update_time,
65+
'status' => $this->status,
66+
]);
67+
68+
$query->andFilterWhere(['like', 'name', $this->name])
69+
->andFilterWhere(['like', 'title', $this->title])
70+
->andFilterWhere(['like', 'content', $this->content]);
71+
72+
return $dataProvider;
73+
}
74+
}

backend/views/page/_form.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
use yii\helpers\Html;
4+
use common\core\ActiveForm;
5+
use common\helpers\ArrayHelper;
6+
use yii\helpers\Url;
7+
8+
/* @var $this yii\web\View */
9+
/* @var $model backend\models\Page */
10+
/* @var $form common\core\ActiveForm */
11+
?>
12+
13+
<?php $form = ActiveForm::begin([
14+
'options'=>[
15+
'class'=>"form-aaa ",
16+
'enctype'=>"multipart/form-data",
17+
]
18+
]); ?>
19+
20+
<?=$form->field($model, 'name')->textInput(['class'=>'form-control c-md-2'])->label('文章标识')->hint('英文标识,只允许含有:英文、数字和中划线');?>
21+
22+
<?=$form->field($model, 'title')->textInput(['class'=>'form-control c-md-5'])->label('文章标题')->hint('单页文章标题');?>
23+
24+
<?=$form->field($model, 'content')->widget('\kucha\ueditor\UEditor',[
25+
'clientOptions' => [
26+
'serverUrl' => Url::to(['/public/ueditor']),//确保serverUrl正确指向后端地址
27+
'lang' =>'zh-cn', //中文为 zh-cn
28+
'initialFrameWidth' => '100%',
29+
'initialFrameHeight' => '400',
30+
//定制菜单,参考http://fex.baidu.com/ueditor/#start-toolbar
31+
'toolbars' => [
32+
[
33+
'fullscreen', 'source', 'undo', 'redo', '|',
34+
'fontsize',
35+
'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'removeformat',
36+
'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|',
37+
'forecolor', 'backcolor', '|',
38+
'lineheight', '|',
39+
'indent', '|',
40+
],
41+
['preview','simpleupload','insertimage','link','emotion','map','insertvideo','insertcode',]
42+
]
43+
]
44+
],['class'=>'c-md-7'])->label('文章内容');?>
45+
46+
<?=$form->field($model, 'status')->radioList(['1'=>'正常','0'=>'隐藏'])->label('状态') ?>
47+
48+
<div class="form-actions">
49+
<?= Html::submitButton('<i class="icon-ok"></i> 确定', ['class' => 'btn blue ajax-post','target-form'=>'form-aaa']) ?>
50+
<?= Html::button('取消', ['class' => 'btn']) ?>
51+
</div>
52+
53+
<?php ActiveForm::end(); ?>
54+

backend/views/page/_search.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use yii\helpers\Html;
4+
use yii\widgets\ActiveForm;
5+
6+
/* @var $this yii\web\View */
7+
/* @var $model backend\models\search\AdSearch */
8+
/* @var $form yii\widgets\ActiveForm */
9+
?>
10+
11+
<?php $form = ActiveForm::begin([
12+
'action' => ['index'],
13+
'method' => 'get',
14+
'options'=>[
15+
//'class'=>"form-inline",
16+
'data-pjax' => true, //开启pjax搜索
17+
]
18+
]); ?>
19+
<div class="row">
20+
<div class="col-md-2">
21+
<?= $form->field($model, 'title')->textInput()->label('标题') ?>
22+
</div>
23+
<div class="col-md-1">
24+
<?=$form->field($model, 'status')->dropDownList([''=>'全部',0=>'隐藏',1=>'正常'],['class'=>'form-control'])->label('状态'); ?>
25+
</div>
26+
27+
<div class="col-md-2">
28+
<div class="form-group" style="margin-top: 24px;">
29+
<?= Html::submitButton('搜索', ['class' => 'btn btn-primary']) ?>
30+
<?= Html::resetButton('重置', ['class' => 'btn btn-default']) ?>
31+
</div>
32+
</div>
33+
</div>
34+
<?php ActiveForm::end(); ?>
35+

backend/views/page/edit.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
use common\core\ActiveForm;
4+
5+
/* @var $this yii\web\View */
6+
/* @var $model backend\models\Menu */
7+
/* @var $form ActiveForm */
8+
9+
/* ===========================以下为本页配置信息================================= */
10+
/* 页面基本属性 */
11+
$this->title = ($this->context->action->id == 'add' ? '添加' : '编辑') . \Yii::t('backend', '文章');
12+
$this->params['title_sub'] = ''; // 在\yii\base\View中有$params这个可以在视图模板中共享的参数
13+
14+
?>
15+
16+
<div class="portlet light bordered">
17+
<div class="portlet-title">
18+
<div class="caption font-red-sunglo">
19+
<i class="icon-settings font-red-sunglo"></i>
20+
<span class="caption-subject bold uppercase"> 内容信息</span>
21+
</div>
22+
<div class="actions">
23+
<div class="btn-group">
24+
<a class="btn btn-sm green dropdown-toggle" href="javascript:;" data-toggle="dropdown"> 工具箱
25+
<i class="fa fa-angle-down"></i>
26+
</a>
27+
<ul class="dropdown-menu pull-right" role="menu">
28+
<li><a href="javascript:;"><i class="fa fa-pencil"></i> 导出Excel </a></li>
29+
<li class="divider"> </li>
30+
<li><a href="javascript:;"> 其他 </a></li>
31+
</ul>
32+
</div>
33+
</div>
34+
</div>
35+
<div class="portlet-body form">
36+
<!-- BEGIN FORM-->
37+
<?= $this->render('_form', [
38+
'model' => $model,
39+
]) ?>
40+
<!-- END FORM-->
41+
</div>
42+
</div>
43+
44+
<!-- 定义数据块 -->
45+
<?php $this->beginBlock('test'); ?>
46+
47+
$(function() {
48+
/* 子导航高亮 */
49+
highlight_subnav('page/index');
50+
});
51+
52+
<?php $this->endBlock() ?>
53+
<!-- 将数据块 注入到视图中的某个位置 -->
54+
<?php $this->registerJs($this->blocks['test'], \yii\web\View::POS_END); ?>

0 commit comments

Comments
 (0)