1
|
|
|
<?php |
2
|
|
|
namespace puck; |
3
|
|
|
|
4
|
|
|
class Controller |
5
|
|
|
{ |
6
|
|
|
|
7
|
|
|
protected $viewPath=''; |
8
|
|
|
protected $title = ''; |
9
|
|
|
private $twig; |
10
|
|
|
private $tVar=array(); |
11
|
|
|
|
12
|
|
|
public function __construct() |
13
|
|
|
{ |
14
|
|
|
$loader=new \Twig_Loader_Filesystem(BASE_PATH.$this->viewPath); |
15
|
|
|
$twig=new \Twig_Environment($loader, array( |
16
|
|
|
'debug' => DEBUG, |
17
|
|
|
'cache' => BASE_PATH.'/cache', |
18
|
|
|
)); |
19
|
|
|
$this->twig=$twig; |
20
|
|
|
$this->initTwigFilter(); |
21
|
|
|
$this->initTwigFunction(); |
22
|
|
|
$this->db=\MysqliDb::getInstance(); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
private function initTwigFilter() { |
25
|
|
|
$filter=new \Twig_SimpleFilter('long2ip', 'long2ip'); |
26
|
|
|
$this->twig->addFilter($filter); |
27
|
|
|
} |
28
|
|
|
private function initTwigFunction() { |
29
|
|
|
$function=new \Twig_SimpleFunction('I', 'I'); |
30
|
|
|
$this->twig->addFunction($function); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
protected function show($tmpPath='') |
34
|
|
|
{ |
35
|
|
|
if ($tmpPath == '') { |
36
|
|
|
if (defined("CONTROLLER_NAME") && defined("ACTION_NAME")) { |
37
|
|
|
$tmpPath=parse_name(CONTROLLER_NAME).'/'.parse_name(ACTION_NAME); |
38
|
|
|
} else { |
39
|
|
|
show_json($this->tVar); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
header('Content-Type:text/html; charset=utf-8'); |
43
|
|
|
header('Cache-control: private'); // 页面缓存控制 |
44
|
|
|
header('X-Powered-By:ViviAnAuthSystem'); |
45
|
|
|
$this->assign('title', $this->title); |
46
|
|
|
echo $this->twig->render($tmpPath.'.'.TempExt, $this->tVar); |
47
|
|
|
die(); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $name |
52
|
|
|
*/ |
53
|
|
|
protected function assign($name, $value = '') { |
54
|
|
|
if (is_array($name)) { |
55
|
|
|
$this->tVar = array_merge($this->tVar, $name); |
56
|
|
|
} else { |
57
|
|
|
$this->tVar[$name] = $value; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function model($modelName, $vars = []) { |
62
|
|
|
return app($modelName . '_model', $vars); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
} |
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: