4 * Base controller class
6 * This class is inherited by all script controllers. All methods of this class
7 * are reserved - there can't be actions with that names (for instance, there
8 * can't be execute actions in real controllers).
11 * @http://www.projectpier.org/
17 * This controller name
21 private $controller_name;
24 * Action that was (or need to be) executed
31 * System controller class. System controller class is class withch methods
32 * are reserved (can't be called). Basic system controllers are Controller and
33 * PageController classes.
37 private $system_controller_class;
40 * Contruct controller and set controller name
46 function __construct() {
48 // Get controller name (tableized classname) and controller class
49 $this->setControllerName( Env
::getControllerName( get_class($this) ) );
50 $this->setSystemControllerClass('Controller');
55 * Execute specific controller action
58 * @param string $action
59 * @return InvalidControllerActionError if action name is not valid or true
61 function execute($action) {
63 // Prepare action name
64 $action = trim(strtolower($action));
66 // If we have valid action execute and done... Else throw exception
67 if($this->validAction($action)) {
68 $this->setAction($action);
72 throw new InvalidControllerActionError($this->getControllerName(), $action);
78 * Forward to specific contrller / action
81 * @param string $action
82 * @param string $controller. If this value is NULL current controller will be
86 function forward($action, $controller = null) {
87 return empty($controller) ?
$this->execute($action) : Env
::executeAction($controller, $action);
91 * Check if specific $action is valid controller action (method exists and it is not reserved)
94 * @param string $action
95 * @return boolean or Error
97 function validAction($action) {
99 // Get reserved names and check action name...
100 $reserved_names = Controller
::getReservedActionNames();
101 if(is_array($reserved_names) && in_array($action, $reserved_names)) return false;
103 // Get methods of this class...
104 $methods = get_class_methods(get_class($this));
106 // If we don't have defined action return false
107 if(!in_array($action, $methods)) return false;
114 // -------------------------------------------------------
115 // Getters and setters
116 // -------------------------------------------------------
119 * Get controller_name
125 function getControllerName() {
126 return $this->controller_name
;
127 } // getControllerName
130 * Set controller_name value
133 * @param string $value
136 function setControllerName($value) {
137 $this->controller_name
= $value;
138 } // setControllerName
147 function getAction() {
148 return $this->action
;
155 * @param string $value
158 function setAction($value) {
159 $this->action
= $value;
163 * Get system_controller_class
169 function getSystemControllerClass() {
170 return $this->system_controller_class
;
171 } // getSystemControllerClass
174 * Set system_controller_class value
177 * @param string $value
180 function setSystemControllerClass($value) {
181 $this->system_controller_class
= $value;
182 } // setSystemControllerClass
185 * Return reserved action names (methods of controller class)
191 function getReservedActionNames() {
194 // Get and check controller class
195 $controller_class = $this->getSystemControllerClass();
196 if(!class_exists($controller_class)) throw new Error("Controller class '$controller_class' does not exists");
198 // If we don't have names get them
199 if(is_null($names)) {
200 $names = get_class_methods($controller_class);
201 foreach($names as $k => $v) $names[$k] = strtolower($v);
207 } // getReservedActionNames