summaryrefslogtreecommitdiffstats
path: root/system/classes/controller.php
diff options
context:
space:
mode:
Diffstat (limited to 'system/classes/controller.php')
-rw-r--r--system/classes/controller.php84
1 files changed, 84 insertions, 0 deletions
diff --git a/system/classes/controller.php b/system/classes/controller.php
new file mode 100644
index 0000000..e543942
--- /dev/null
+++ b/system/classes/controller.php
@@ -0,0 +1,84 @@
+<?php
+
+/**
+ * Base Controller class. Controlers contain the logic of your website,
+ * each action representing a reply to a prticular request, e.g. a single page.
+ */
+class Controller {
+
+ /**
+ * Request for this controller. Holds all input data.
+ * @var Request
+ * @access public
+ */
+ public $request;
+
+ /**
+ * Response for this controller. It will be updated with headers and
+ * response body during controller execution
+ * @var Response
+ * @access public
+ */
+ public $response;
+
+ /**
+ * If set to False stops controller execution
+ * @var boolean
+ * @access public
+ */
+ public $execute=true;
+
+ /**
+ * This method is called before the action.
+ * You can override it if you need to,
+ * it doesn't do anything by default.
+ *
+ * @return void
+ * @access public
+ */
+ public function before() {}
+
+ /**
+ * This method is called after the action.
+ * You can override it if you need to,
+ * it doesn't do anything by default.
+ *
+ * @return void
+ * @access public
+ */
+ public function after() { }
+
+ /**
+ * Creates new Controller
+ *
+ * @return void
+ * @access public
+ */
+ public function __construct() {
+ $this->response=new Response;
+ }
+
+ /**
+ * Runs the appropriate action.
+ * It will axecute the before() method before the action
+ * and after() method after the action finishes.
+ *
+ * @param string $action Name of the action to execute.
+ * @return void
+ * @access public
+ * @throws Exception If the specified action doesn't exist
+ */
+ public function run($action) {
+ $action = 'action_'.$action;
+ if (!method_exists($this, $action))
+ throw new Exception("Method {$action} doesn't exist in ".get_class($this));
+ $this->execute=true;
+ $this->before();
+ if($this->execute)
+ $this->$action();
+ if($this->execute)
+ $this->after();
+ }
+
+
+} \ No newline at end of file