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\Mvc\Models\Query; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class PostController |
12
|
|
|
* @package App\Modules\Blog\Controllers |
13
|
|
|
*/ |
14
|
|
|
class PostController extends Controller |
15
|
|
|
{ |
16
|
|
|
public function filters() |
17
|
|
|
{ |
18
|
|
|
return [ |
19
|
|
|
[ |
20
|
|
|
'class' => '\Micro\Filter\AccessFilter', |
21
|
|
|
'actions' => ['index', 'view', 'create', 'update', 'delete'], |
22
|
|
|
'rules' => [ |
23
|
|
|
[ |
24
|
|
|
'allow' => false, |
25
|
|
|
'actions' => ['create', 'update', 'delete'], |
26
|
|
|
'users' => ['?'], |
27
|
|
|
'message' => 'Only for authorized!' |
28
|
|
|
], |
29
|
|
|
[ |
30
|
|
|
'allow' => true, |
31
|
|
|
'actions' => ['index', 'view'], |
32
|
|
|
'users' => ['*'], |
33
|
|
|
'message' => 'View for all' |
34
|
|
|
] |
35
|
|
|
] |
36
|
|
|
], |
37
|
|
|
[ |
38
|
|
|
'class' => '\Micro\Filter\CsrfFilter', |
39
|
|
|
'actions' => ['login'] |
40
|
|
|
], |
41
|
|
|
[ |
42
|
|
|
'class' => '\Micro\Filter\XssFilter', |
43
|
|
|
'actions' => ['index', 'login', 'logout'], |
44
|
|
|
'clean' => '*' |
45
|
|
|
] |
46
|
|
|
]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function actionIndex() |
50
|
|
|
{ |
51
|
|
|
$crt = new Query($this->container->db); |
|
|
|
|
52
|
|
|
$crt->table = Blog::tableName(); |
53
|
|
|
$crt->order = 'id DESC'; |
54
|
|
|
|
55
|
|
|
$v = new View($this->container); |
56
|
|
|
$v->addParameter('blogs', $crt); |
57
|
|
|
$v->addParameter('page', $this->container->request->query('page') ?: 0); |
|
|
|
|
58
|
|
|
|
59
|
|
|
return $v; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
View Code Duplication |
public function actionView() |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$blog = Blog::findByPk($this->container->request->query('id'), $this->container); |
|
|
|
|
65
|
|
|
$v = new View($this->container); |
66
|
|
|
$v->addParameter('model', $blog); |
67
|
|
|
|
68
|
|
|
return $v; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function actionCreate() |
72
|
|
|
{ |
73
|
|
|
$blog = new Blog($this->container); |
74
|
|
|
|
75
|
|
|
/** @var array $blogData */ |
76
|
|
|
if ($blogData = $this->container->request->post('Blog')) { |
|
|
|
|
77
|
|
|
$blog->name = $blogData['name']; |
78
|
|
|
$blog->content = $blogData['content']; |
79
|
|
|
|
80
|
|
|
if ($blog->save()) { |
81
|
|
|
$this->redirect('/blog/post/' . $blog->id); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$v = new View($this->container); |
86
|
|
|
$v->addParameter('model', $blog); |
87
|
|
|
|
88
|
|
|
return $v; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
View Code Duplication |
public function actionUpdate() |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$blog = Blog::findByPk($this->container->request->query('id'), $this->container); |
|
|
|
|
94
|
|
|
|
95
|
|
|
$blog->name = 'setup-er'; |
96
|
|
|
|
97
|
|
|
return $blog->save(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function actionDelete() |
101
|
|
|
{ |
102
|
|
|
$blog = Blog::findByPk( |
103
|
|
|
$this->container->request->query('id'), |
|
|
|
|
104
|
|
|
$this->container |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
return $blog->delete(); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: