-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControllerFront.php
More file actions
145 lines (114 loc) · 3.6 KB
/
ControllerFront.php
File metadata and controls
145 lines (114 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* Description of ControllerFront
*
* @author gullo
*/
class MyFw_ControllerFront {
static private $instance = null;
private $_controller;
private $_action;
private $_params;
private $_router;
private $_app_env;
private $_app_config;
function __construct($env, $config) {
$this->_app_env = $env;
$this->_app_config = $config;
// include and start autoloader
include_once("Autoloader.php");
Autoloader::Register();
// set Instance
self::$instance = $this;
// start Router class
$this->_router = new MyFw_Router(filter_input(INPUT_SERVER, "REQUEST_URI"));
}
// SINGLETON
static public function getInst() {
if( is_null(self::$instance) ) {
throw new MyFw_Exception("You need to create the FIRST INSTANCE of ControllerFront!");
}
return self::$instance;
}
public function setRouter($router) {
$this->_router = $router;
}
public function bootstrap() {
$bs = new Bootstrap();
$bs->run();
return $this;
}
public function bootstrap_api() {
$bs = new Bootstrap();
$bs->run_api();
return $this;
}
public function run() {
$this->_controller = $this->_router->getController();
$this->_action = $this->_router->getAction();
$this->_params = $this->_router->getParams();
$this->invokePluginsMethods('preDispatch');
$this->_dispatch();
$this->invokePluginsMethods('postDispatch');
$layout = Zend_Registry::get("layout");
$layout->display();
}
public function getController() {
return $this->_controller;
}
public function getAction() {
return $this->_action;
}
public function getParams() {
return $this->_params;
}
public function setController($c) {
$this->_controller = $c;
}
public function setAction($a) {
$this->_action = $a;
}
public function setParams($arP) {
$this->_params = $arP;
}
private function _dispatch() {
$cObj = $this->invokeControllerAction();
if($cObj instanceof MyFw_Controller) {
$cObj->initContent();
} else {
throw new MyFw_Exception("ERROR: Controller is NOT an object!");
}
}
function invokeControllerAction()
{
try {
$controllerName = "Controller_".$this->_controller;
if(class_exists($controllerName)) {
$controllerObj = new $controllerName;
$actionName = $this->_action."Action";
//echo "<br>ActionController call -> $controllerName :: $actionName";
// Invoke Action and return the Controller Object
if(method_exists($controllerObj, $actionName)) {
$controllerObj->$actionName();
return $controllerObj;
}
}
} catch (MyFw_Exception $exc) {
$exc->displayError();
}
// ERROR 404
$this->_controller = 'Index';
$this->_action = 'error';
$this->_params = array("code" => 404);
return $this->invokeControllerAction();
}
private function invokePluginsMethods($method) {
$plugin = Zend_Registry::get('plugin');
$ppObj = $plugin->getAllPluginsInstances();
if(!is_null($ppObj)) {
foreach($ppObj AS $name => $instance) {
$instance->$method($this);
}
}
}
}