1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Modules\Blog\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Components\Controller; |
6
|
|
|
use App\Components\View; |
7
|
|
|
use App\Modules\Blog\Models\Blog; |
8
|
|
|
use Micro\Db\Injector; |
9
|
|
|
use Micro\Mvc\Models\Query; |
10
|
|
|
use Micro\Web\RequestInjector; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class PostController |
14
|
|
|
* @package App\Modules\Blog\Controllers |
15
|
|
|
*/ |
16
|
|
|
class PostController extends Controller |
17
|
|
|
{ |
18
|
|
|
public function filters() |
19
|
|
|
{ |
20
|
|
|
return [ |
21
|
|
|
[ |
22
|
|
|
'class' => '\Micro\Filter\AccessFilter', |
23
|
|
|
'actions' => ['index', 'view', 'create', 'update', 'delete'], |
24
|
|
|
'rules' => [ |
25
|
|
|
[ |
26
|
|
|
'allow' => false, |
27
|
|
|
'actions' => ['create', 'update', 'delete'], |
28
|
|
|
'users' => ['?'], |
29
|
|
|
'message' => 'Only for authorized!' |
30
|
|
|
], |
31
|
|
|
[ |
32
|
|
|
'allow' => true, |
33
|
|
|
'actions' => ['index', 'view'], |
34
|
|
|
'users' => ['*'], |
35
|
|
|
'message' => 'View for all' |
36
|
|
|
] |
37
|
|
|
] |
38
|
|
|
], |
39
|
|
|
[ |
40
|
|
|
'class' => '\Micro\Filter\CsrfFilter', |
41
|
|
|
'actions' => ['login'] |
42
|
|
|
], |
43
|
|
|
[ |
44
|
|
|
'class' => '\Micro\Filter\XssFilter', |
45
|
|
|
'actions' => ['index', 'login', 'logout'], |
46
|
|
|
'clean' => '*' |
47
|
|
|
] |
48
|
|
|
]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function actionIndex() |
52
|
|
|
{ |
53
|
|
|
$crt = new Query((new Injector)->getDriver()); |
54
|
|
|
$crt->table = Blog::tableName(); |
55
|
|
|
$crt->order = 'id DESC'; |
56
|
|
|
|
57
|
|
|
$v = new View(); |
58
|
|
|
$v->addParameter('blogs', $crt); |
59
|
|
|
$query = (new RequestInjector)->build()->getQueryParams(); |
60
|
|
|
$v->addParameter('page', $query['page'] ?: 0); |
61
|
|
|
|
62
|
|
|
return $v; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function actionView() |
66
|
|
|
{ |
67
|
|
|
$query = (new RequestInjector)->build()->getQueryParams(); |
68
|
|
|
$blog = Blog::findByPk($query['id']); |
69
|
|
|
$v = new View(); |
70
|
|
|
$v->addParameter('model', $blog); |
71
|
|
|
|
72
|
|
|
return $v; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function actionCreate() |
76
|
|
|
{ |
77
|
|
|
$blog = new Blog(); |
78
|
|
|
|
79
|
|
|
$body = (new RequestInjector)->build()->getParsedBody(); |
80
|
|
|
/** @var array $blogData */ |
81
|
|
|
if ($blogData = $body['Blog']) { |
82
|
|
|
$blog->name = $blogData['name']; |
83
|
|
|
$blog->content = $blogData['content']; |
84
|
|
|
|
85
|
|
|
if ($blog->save()) { |
86
|
|
|
return $this->redirect('/blog/post/' . $blog->id); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$v = new View(); |
91
|
|
|
$v->addParameter('model', $blog); |
92
|
|
|
|
93
|
|
|
return $v; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
View Code Duplication |
public function actionUpdate() |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$query = (new RequestInjector)->build()->getQueryParams(); |
99
|
|
|
$blog = Blog::findByPk($query['id']); |
100
|
|
|
|
101
|
|
|
$blog->name = 'setup-er'; |
102
|
|
|
|
103
|
|
|
return $blog->save(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
View Code Duplication |
public function actionDelete() |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$query = (new RequestInjector)->build()->getQueryParams(); |
109
|
|
|
$blog = Blog::findByPk($query['id']); |
110
|
|
|
|
111
|
|
|
return $blog->delete(); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.