Skip to content

Commit 622a9d1

Browse files
author
igor-chepurnoi
committed
add new properties to CmsController, rename cms search class
1 parent 5abfdb3 commit 622a9d1

File tree

5 files changed

+68
-23
lines changed

5 files changed

+68
-23
lines changed

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,19 @@ php yii migrate --migrationPath=@vendor/yii2mod/yii2-cms/migrations
4040
'modules' => [
4141
'admin' => [
4242
'controllerMap' => [
43-
'cms' => 'yii2mod\cms\controllers\CmsController'
44-
// You can set your template files
45-
'layout' => '@app/modules/backend/views/layouts/main',
46-
'viewPath' => '@app/modules/backend/views/cms/'
43+
'cms' => 'yii2mod\cms\controllers\CmsController',
44+
// Also you can override some controller properties.
45+
'cms' => [
46+
'class' => 'yii2mod\cms\controllers\CmsController',
47+
'searchClass' => [
48+
'class' => 'yii2mod\cms\models\search\CmsSearch',
49+
'pageSize' => 25
50+
],
51+
'modelClass' => 'Your own cms model class',
52+
'indexView' => 'custom path to index view file',
53+
'createView' => 'custom path to create view file',
54+
'updateView' => 'custom path to update view file',
55+
],
4756
],
4857
],
4958
],

controllers/CmsController.php

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use yii\web\Controller;
88
use yii\web\NotFoundHttpException;
99
use yii\filters\VerbFilter;
10-
use yii2mod\cms\models\search\CmsModelSearch;
1110
use yii2mod\editable\EditableAction;
1211
use yii2mod\toggle\actions\ToggleAction;
1312

@@ -18,9 +17,29 @@
1817
class CmsController extends Controller
1918
{
2019
/**
21-
* @var string view path
20+
* @var string path to index view file, which is used in admin panel
2221
*/
23-
public $viewPath = '@vendor/yii2mod/yii2-cms/views/cms/';
22+
public $indexView = '@vendor/yii2mod/yii2-cms/views/cms/index';
23+
24+
/**
25+
* @var string path to create view file, which is used in admin panel
26+
*/
27+
public $createView = '@vendor/yii2mod/yii2-cms/views/cms/create';
28+
29+
/**
30+
* @var string path to update view file, which is used in admin panel
31+
*/
32+
public $updateView = '@vendor/yii2mod/yii2-cms/views/cms/update';
33+
34+
/**
35+
* @var string search class name for searching
36+
*/
37+
public $searchClass = 'yii2mod\cms\models\search\CmsSearch';
38+
39+
/**
40+
* @var string model class name for CRUD operations
41+
*/
42+
public $modelClass = 'yii2mod\cms\models\CmsModel';
2443

2544
/**
2645
* @inheritdoc
@@ -60,41 +79,45 @@ public function actions()
6079

6180
/**
6281
* Lists all CmsModel models.
82+
*
6383
* @return mixed
6484
*/
6585
public function actionIndex()
6686
{
67-
$searchModel = new CmsModelSearch();
87+
$searchModel = Yii::createObject($this->searchClass);
6888
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
6989

70-
return $this->render($this->viewPath . 'index', [
90+
return $this->render($this->indexView, [
7191
'dataProvider' => $dataProvider,
7292
'searchModel' => $searchModel
7393
]);
7494
}
7595

7696
/**
7797
* Creates a new CmsModel model.
78-
* If creation is successful, the browser will be redirected to the 'view' page.
98+
*
99+
* If creation is successful, the browser will be redirected to the 'index' page.
100+
*
79101
* @return mixed
80102
*/
81103
public function actionCreate()
82104
{
83-
$model = new CmsModel();
105+
$model = Yii::createObject($this->modelClass);
84106

85107
if ($model->load(Yii::$app->request->post()) && $model->save()) {
86108
Yii::$app->session->setFlash('success', Yii::t('yii2mod.cms', 'Page has been created.'));
87109
return $this->redirect(['index']);
88110
}
89111

90-
return $this->render($this->viewPath . 'create', [
91-
'model' => $model,
112+
return $this->render($this->createView, [
113+
'model' => $model
92114
]);
93115
}
94116

95117
/**
96118
* Updates an existing CmsModel model.
97-
* If update is successful, the browser will be redirected to the 'view' page.
119+
*
120+
* If update is successful, the browser will be redirected to the 'index' page.
98121
*
99122
* @param integer $id
100123
*
@@ -108,13 +131,15 @@ public function actionUpdate($id)
108131
Yii::$app->session->setFlash('success', Yii::t('yii2mod.cms', 'Page has been updated.'));
109132
return $this->redirect(['index']);
110133
}
111-
return $this->render($this->viewPath . 'update', [
112-
'model' => $model,
134+
135+
return $this->render($this->updateView, [
136+
'model' => $model
113137
]);
114138
}
115139

116140
/**
117141
* Deletes an existing CmsModel model.
142+
*
118143
* If deletion is successful, the browser will be redirected to the 'index' page.
119144
*
120145
* @param integer $id
@@ -135,15 +160,17 @@ public function actionDelete($id)
135160
* @param integer $id
136161
*
137162
* @return CmsModel the loaded model
163+
*
138164
* @throws NotFoundHttpException if the model cannot be found
139165
*/
140166
protected function findModel($id)
141167
{
142-
if (($model = CmsModel::findOne($id)) !== null) {
168+
$cmsModel = $this->modelClass;
169+
170+
if (($model = $cmsModel::findOne($id)) !== null) {
143171
return $model;
144172
} else {
145173
throw new NotFoundHttpException(Yii::t('yii2mod.cms', 'The requested page does not exist.'));
146174
}
147175
}
148-
149-
}
176+
}

models/CmsModel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public static function find()
9999
* Find page by url
100100
*
101101
* @param $url
102+
*
102103
* @return array|null|ActiveRecord
103104
*/
104105
public function findPage($url)
@@ -124,6 +125,7 @@ public function getContent()
124125
* Replaces widget short code on appropriate widget
125126
*
126127
* @param $data
128+
*
127129
* @return string
128130
*/
129131
private function replace($data)

models/search/CmsModelSearch.php renamed to models/search/CmsSearch.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66
use yii2mod\cms\models\CmsModel;
77

88
/**
9-
* Class CmsModelSearch
9+
* Class CmsSearch
1010
* @package yii2mod\cms\models\search
1111
*/
12-
class CmsModelSearch extends CmsModel
12+
class CmsSearch extends CmsModel
1313
{
14+
/**
15+
* @var int the default page size.
16+
*/
17+
public $pageSize = 10;
18+
1419
/**
1520
* @inheritdoc
1621
*/
@@ -35,7 +40,7 @@ public function search($params)
3540
$dataProvider = new ActiveDataProvider([
3641
'query' => $query,
3742
'pagination' => [
38-
'pageSize' => 10
43+
'pageSize' => $this->pageSize
3944
]
4045
]);
4146

tests/PageActionTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ public function testViewPage()
3131
$this->assertEquals('about-us', $response['params']['model']['url']);
3232
}
3333

34+
/**
35+
* @expectedException \yii\web\NotFoundHttpException
36+
*/
3437
public function testViewNotExistPage()
3538
{
36-
$this->setExpectedException('yii\web\NotFoundHttpException');
3739
$this->runAction(['pageId' => 'not exist page']);
3840
}
3941

0 commit comments

Comments
 (0)