Skip to content

Commit ba9521f

Browse files
committed
refactoring
1 parent 2a3a169 commit ba9521f

File tree

2 files changed

+73
-102
lines changed

2 files changed

+73
-102
lines changed

yii2-app-advanced/backend/controllers/InterviewController.php

Lines changed: 17 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
use Yii;
66
use common\models\Interview;
77
use yii\data\ActiveDataProvider;
8+
use yii\filters\AccessControl;
89
use yii\web\Controller;
910
use yii\web\NotFoundHttpException;
10-
use yii\filters\VerbFilter;
1111

1212
/**
1313
* InterviewController implements the CRUD actions for Interview model.
@@ -17,10 +17,14 @@ class InterviewController extends Controller
1717
public function behaviors()
1818
{
1919
return [
20-
'verbs' => [
21-
'class' => VerbFilter::className(),
22-
'actions' => [
23-
'delete' => ['post'],
20+
'access' => [
21+
'class' => AccessControl::className(),
22+
'rules' => [
23+
[
24+
'actions' => ['delete', 'index'],
25+
'allow' => true,
26+
'roles' => ['@'],
27+
],
2428
],
2529
],
2630
];
@@ -34,61 +38,21 @@ public function actionIndex()
3438
{
3539
$dataProvider = new ActiveDataProvider([
3640
'query' => Interview::find(),
41+
'pagination' => [
42+
'pageSize' => 50,
43+
],
44+
'sort' => [
45+
'defaultOrder' => [
46+
'name' => SORT_ASC,
47+
]
48+
]
3749
]);
3850

3951
return $this->render('index', [
4052
'dataProvider' => $dataProvider,
4153
]);
4254
}
4355

44-
/**
45-
* Displays a single Interview model.
46-
* @param integer $id
47-
* @return mixed
48-
*/
49-
public function actionView($id)
50-
{
51-
return $this->render('view', [
52-
'model' => $this->findModel($id),
53-
]);
54-
}
55-
56-
/**
57-
* Creates a new Interview model.
58-
* If creation is successful, the browser will be redirected to the 'view' page.
59-
* @return mixed
60-
*/
61-
public function actionCreate()
62-
{
63-
$model = new Interview();
64-
65-
if ($model->load(Yii::$app->request->post()) && $model->save()) {
66-
return $this->redirect(['view', 'id' => $model->id]);
67-
} else {
68-
return $this->render('create', [
69-
'model' => $model,
70-
]);
71-
}
72-
}
73-
74-
/**
75-
* Updates an existing Interview model.
76-
* If update is successful, the browser will be redirected to the 'view' page.
77-
* @param integer $id
78-
* @return mixed
79-
*/
80-
public function actionUpdate($id)
81-
{
82-
$model = $this->findModel($id);
83-
84-
if ($model->load(Yii::$app->request->post()) && $model->save()) {
85-
return $this->redirect(['view', 'id' => $model->id]);
86-
} else {
87-
return $this->render('update', [
88-
'model' => $model,
89-
]);
90-
}
91-
}
9256

9357
/**
9458
* Deletes an existing Interview model.

yii2-app-advanced/backend/views/interview/index.php

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,62 +8,69 @@
88

99
$this->title = 'Опрос';
1010
$this->params['breadcrumbs'][] = $this->title;
11-
$planets = ['Меркурий', 'Венера', 'Земля', 'Марс', 'Юпитер', 'Сатурн', 'Уран', 'Нептун'];
12-
$astronauts = [
13-
'Юрий Гагарин',
14-
'Алексей Леонов',
15-
'Нил Армстронг',
16-
'Валентина Терешкова',
17-
'Эдвин Олдрин',
18-
'Анатолий Соловьев'
19-
];
2011
?>
2112
<div class="interview-index">
2213

2314
<h1><?= Html::encode($this->title) ?></h1>
2415

25-
<?= GridView::widget([
26-
'dataProvider' => $dataProvider,
27-
'columns' => [
28-
['class' => 'yii\grid\SerialColumn'],
29-
'name',
30-
[
31-
'attribute' => 'sex',
32-
'value' => function ($model) {
33-
return $model->sex ? 'Мужчина' : 'Женщина';
34-
}
35-
],
36-
[
37-
'attribute' => 'planets',
38-
'value' => function ($model) use ($planets) {
39-
$result = null;
40-
$numbers = explode(',', $model->planets);
41-
foreach ($numbers as $number) {
42-
$result .= $planets[$number] . ' ';
16+
<?php
17+
$planets = ['Меркурий', 'Венера', 'Земля', 'Марс', 'Юпитер', 'Сатурн', 'Уран', 'Нептун'];
18+
19+
$astronauts = [
20+
'Юрий Гагарин',
21+
'Алексей Леонов',
22+
'Нил Армстронг',
23+
'Валентина Терешкова',
24+
'Эдвин Олдрин',
25+
'Анатолий Соловьев'
26+
];
27+
28+
echo GridView::widget(
29+
[
30+
'dataProvider' => $dataProvider,
31+
'columns' => [
32+
['class' => 'yii\grid\SerialColumn'],
33+
'name',
34+
[
35+
'attribute' => 'sex',
36+
'value' => function ($model) {
37+
return $model->sex ? 'Мужчина' : 'Женщина';
4338
}
44-
return $result;
45-
}
46-
],
47-
[
48-
'attribute' => 'astronauts',
49-
'value' => function ($model) use ($astronauts) {
50-
$result = null;
51-
$numbers = explode(',', $model->astronauts);
52-
foreach ($numbers as $number) {
53-
$result .= $astronauts[$number] . ' ';
39+
],
40+
[
41+
'attribute' => 'planets',
42+
'value' => function ($model) use ($planets) {
43+
$result = null;
44+
$numbers = explode(',', $model->planets);
45+
foreach ($numbers as $number) {
46+
$result .= $planets[$number] . ' ';
47+
}
48+
return $result;
5449
}
55-
return $result;
56-
}
57-
],
58-
[
59-
'attribute' => 'planet',
60-
'value' => function ($model) use ($planets) {
61-
return $planets[$model->planet];
62-
}
50+
],
51+
[
52+
'attribute' => 'astronauts',
53+
'value' => function ($model) use ($astronauts) {
54+
$result = null;
55+
$numbers = explode(',', $model->astronauts);
56+
foreach ($numbers as $number) {
57+
$result .= $astronauts[$number] . ' ';
58+
}
59+
return $result;
60+
}
61+
],
62+
[
63+
'attribute' => 'planet',
64+
'value' => function ($model) use ($planets) {
65+
return $planets[$model->planet];
66+
}
67+
],
68+
[
69+
'class' => 'yii\grid\ActionColumn',
70+
'template' => '{delete}',
71+
],
6372
],
64-
65-
['class' => 'yii\grid\ActionColumn'],
66-
],
67-
]); ?>
73+
]
74+
); ?>
6875

6976
</div>

0 commit comments

Comments
 (0)