|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace XHGui; |
|
4
|
|
|
|
|
5
|
|
|
use Slim\Http\Response; |
|
6
|
|
|
use Slim\Slim as App; |
|
7
|
|
|
use Slim\Views\Twig; |
|
8
|
|
|
|
|
9
|
|
|
abstract class AbstractController |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $_templateVars = []; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string|null |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $_template = null; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var App |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $app; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(App $app) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->app = $app; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function set($vars) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->_templateVars = array_merge($this->_templateVars, $vars); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function templateVars() |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->_templateVars; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** @see RenderMiddleware */ |
|
42
|
|
|
public function renderView() |
|
43
|
|
|
{ |
|
44
|
|
|
// We want to render the specified Twig template to the output buffer. |
|
45
|
|
|
// The simplest way to do that is Slim::render, but that is not allowed |
|
46
|
|
|
// in middleware, because it uses Slim\View::display which prints |
|
47
|
|
|
// directly to the native PHP output buffer. |
|
48
|
|
|
// Doing that is problematic, because the HTTP headers set via $app->response() |
|
49
|
|
|
// must be output first, which won't happen until after the middleware |
|
50
|
|
|
// is completed. Output of headers and body is done by the Slim::run entry point. |
|
51
|
|
|
|
|
52
|
|
|
// The below is copied from Slim::render (slim/[email protected]). |
|
53
|
|
|
// Modified to use View::fetch + Response::write, instead of View::display. |
|
54
|
|
|
$this->render($this->_template, $this->_templateVars); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function config(string $key) |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->app->config($key); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function render(string $template, array $data = []) |
|
63
|
|
|
{ |
|
64
|
|
|
/** @var Response $response */ |
|
65
|
|
|
$response = $this->app->response; |
|
66
|
|
|
/** @var Twig $renderer */ |
|
67
|
|
|
$renderer = $this->app->view; |
|
68
|
|
|
|
|
69
|
|
|
$renderer->appendData($data); |
|
70
|
|
|
$body = $renderer->fetch($template); |
|
71
|
|
|
$response->write($body); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|