summaryrefslogtreecommitdiffstats
path: root/system/classes/controller.php
blob: 45160adb24b77af02a39be19f77bea90d7f8e13a (plain)
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
<?php	

/**
 * Base Controller class. Controllers contain the  logic of your website,
 * each action representing a reply to a particular 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 execute 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();
	}
	

}