|
1
|
|
|
<?php |
|
2
|
|
|
namespace puck; |
|
3
|
|
|
|
|
4
|
|
|
class Controller |
|
5
|
|
|
{ |
|
6
|
|
|
|
|
7
|
|
|
protected $viewPath = ''; |
|
8
|
|
|
private $twig; |
|
9
|
|
|
private $tVar = array(); |
|
10
|
|
|
protected $title=''; |
|
11
|
|
|
public function __construct() |
|
12
|
|
|
{ |
|
13
|
|
|
$loader = new \Twig_Loader_Filesystem(BASE_PATH . $this->viewPath); |
|
14
|
|
|
$twig = new \Twig_Environment($loader, array( |
|
15
|
|
|
'debug' => DEBUG, |
|
16
|
|
|
'cache' => BASE_PATH . '/cache', |
|
17
|
|
|
)); |
|
18
|
|
|
$this->twig = $twig; |
|
19
|
|
|
$this->initTwigFilter(); |
|
20
|
|
|
$this->initTwigFunction(); |
|
21
|
|
|
$this->db = \MysqliDb::getInstance(); |
|
|
|
|
|
|
22
|
|
|
} |
|
23
|
|
|
private function initTwigFilter(){ |
|
24
|
|
|
$filter = new \Twig_SimpleFilter('long2ip', 'long2ip'); |
|
25
|
|
|
$this->twig->addFilter($filter); |
|
26
|
|
|
} |
|
27
|
|
|
private function initTwigFunction(){ |
|
28
|
|
|
$function = new \Twig_SimpleFunction('I','I'); |
|
29
|
|
|
$this->twig->addFunction($function); |
|
30
|
|
|
} |
|
31
|
|
|
protected function assign($name, $value = '') |
|
32
|
|
|
{ |
|
33
|
|
|
if (is_array($name)) { |
|
34
|
|
|
$this->tVar = array_merge($this->tVar, $name); |
|
35
|
|
|
} else { |
|
36
|
|
|
$this->tVar[$name] = $value; |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
protected function show($tmpPath = '') |
|
41
|
|
|
{ |
|
42
|
|
|
if ($tmpPath == '') { |
|
43
|
|
|
if (defined("CONTROLLER_NAME") && defined("ACTION_NAME")) { |
|
44
|
|
|
$tmpPath = CONTROLLER_NAME . '/' . ACTION_NAME; |
|
45
|
|
|
} else { |
|
46
|
|
|
show_json($this->tVar); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
header('Content-Type:text/html; charset=utf-8'); |
|
50
|
|
|
header('Cache-control: private'); // 页面缓存控制 |
|
51
|
|
|
header('X-Powered-By:ViviAnAuthSystem'); |
|
52
|
|
|
$this->assign('title',$this->title); |
|
53
|
|
|
echo $this->twig->render($tmpPath . '.' . TempExt, $this->tVar); |
|
54
|
|
|
die(); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: