|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Awurth\SlimValidation\Validator; |
|
6
|
|
|
use Cartalyst\Sentinel\Sentinel; |
|
7
|
|
|
use Psr\Container\ContainerInterface; |
|
8
|
|
|
use Security\Exception\AccessDeniedException; |
|
9
|
|
|
use Slim\Exception\NotFoundException; |
|
10
|
|
|
use Slim\Flash\Messages; |
|
11
|
|
|
use Slim\Http\Request; |
|
12
|
|
|
use Slim\Http\Response; |
|
13
|
|
|
use Slim\Router; |
|
14
|
|
|
use Slim\Views\Twig; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @property Twig view |
|
18
|
|
|
* @property Router router |
|
19
|
|
|
* @property Messages flash |
|
20
|
|
|
* @property Validator validator |
|
21
|
|
|
* @property Sentinel auth |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class Controller |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Slim application container. |
|
27
|
|
|
* |
|
28
|
|
|
* @var ContainerInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $container; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param ContainerInterface $container |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(ContainerInterface $container) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->container = $container; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Gets request parameters. |
|
44
|
|
|
* |
|
45
|
|
|
* @param Request $request |
|
46
|
|
|
* @param string[] $params |
|
47
|
|
|
* @param string $default |
|
48
|
|
|
* |
|
49
|
|
|
* @return string[] |
|
50
|
|
|
*/ |
|
51
|
|
|
public function params(Request $request, array $params, $default = null) |
|
52
|
|
|
{ |
|
53
|
|
|
$data = []; |
|
54
|
|
|
foreach ($params as $param) { |
|
55
|
|
|
$data[$param] = $request->getParam($param, $default); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $data; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Redirects to a route. |
|
63
|
|
|
* |
|
64
|
|
|
* @param Response $response |
|
65
|
|
|
* @param string $route |
|
66
|
|
|
* @param array $params |
|
67
|
|
|
* |
|
68
|
|
|
* @return Response |
|
69
|
|
|
*/ |
|
70
|
|
|
public function redirect(Response $response, $route, array $params = []) |
|
71
|
|
|
{ |
|
72
|
|
|
return $response->withRedirect($this->router->pathFor($route, $params)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Redirects to a url. |
|
77
|
|
|
* |
|
78
|
|
|
* @param Response $response |
|
79
|
|
|
* @param string $url |
|
80
|
|
|
* |
|
81
|
|
|
* @return Response |
|
82
|
|
|
*/ |
|
83
|
|
|
public function redirectTo(Response $response, $url) |
|
84
|
|
|
{ |
|
85
|
|
|
return $response->withRedirect($url); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Writes JSON in the response body. |
|
90
|
|
|
* |
|
91
|
|
|
* @param Response $response |
|
92
|
|
|
* @param mixed $data |
|
93
|
|
|
* @param int $status |
|
94
|
|
|
* |
|
95
|
|
|
* @return Response |
|
96
|
|
|
*/ |
|
97
|
|
|
public function json(Response $response, $data, $status = 200) |
|
98
|
|
|
{ |
|
99
|
|
|
return $response->withJson($data, $status); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Writes text in the response body. |
|
104
|
|
|
* |
|
105
|
|
|
* @param Response $response |
|
106
|
|
|
* @param string $data |
|
107
|
|
|
* @param int $status |
|
108
|
|
|
* |
|
109
|
|
|
* @return int |
|
110
|
|
|
*/ |
|
111
|
|
|
public function write(Response $response, $data, $status = 200) |
|
112
|
|
|
{ |
|
113
|
|
|
return $response->withStatus($status)->getBody()->write($data); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Adds a flash message. |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $name |
|
120
|
|
|
* @param string $message |
|
121
|
|
|
*/ |
|
122
|
|
|
public function flash($name, $message) |
|
123
|
|
|
{ |
|
124
|
|
|
$this->flash->addMessage($name, $message); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Creates a new NotFoundException. |
|
129
|
|
|
* |
|
130
|
|
|
* @param Request $request |
|
131
|
|
|
* @param Response $response |
|
132
|
|
|
* |
|
133
|
|
|
* @return NotFoundException |
|
134
|
|
|
*/ |
|
135
|
|
|
public function notFoundException(Request $request, Response $response) |
|
136
|
|
|
{ |
|
137
|
|
|
return new NotFoundException($request, $response); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Creates a new AccessDeniedException. |
|
142
|
|
|
* |
|
143
|
|
|
* @param Request $request |
|
144
|
|
|
* @param Response $response |
|
145
|
|
|
* |
|
146
|
|
|
* @return AccessDeniedException |
|
147
|
|
|
*/ |
|
148
|
|
|
public function accessDeniedException(Request $request, Response $response) |
|
149
|
|
|
{ |
|
150
|
|
|
return new AccessDeniedException($request, $response); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Gets a service from the container. |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $property |
|
157
|
|
|
* |
|
158
|
|
|
* @return mixed |
|
159
|
|
|
*/ |
|
160
|
|
|
public function __get($property) |
|
161
|
|
|
{ |
|
162
|
|
|
return $this->container->get($property); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|