Rev 27 | Rev 30 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
27 | PointedEar | 1 | <?php |
2 | |||
29 | PointedEar | 3 | require_once __DIR__ . '/Application.php'; |
4 | require_once __DIR__ . '/View.php'; |
||
27 | PointedEar | 5 | |
6 | /* lcfirst() is unavailable before PHP 5.3 */ |
||
7 | if (false === function_exists('lcfirst')) |
||
8 | { |
||
9 | /** |
||
10 | * Make a string's first character lowercase |
||
11 | * |
||
12 | * @param string $str The input string. |
||
13 | * @return string The resulting string. |
||
14 | * @link http://www.php.net/manual/en/function.lcfirst.php |
||
15 | */ |
||
16 | function lcfirst($str) |
||
17 | { |
||
18 | return strtolower(substr($str, 0, 1)) . substr($str, 1); |
||
19 | } |
||
20 | } |
||
21 | |||
22 | /** |
||
23 | * A general controller that can handle views according to |
||
24 | * the MVC pattern |
||
25 | * |
||
26 | * @author tlahn |
||
27 | */ |
||
28 | abstract class Controller |
||
29 | { |
||
30 | /** |
||
31 | * Default action of the controller |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $_defaultAction = 'index'; |
||
35 | |||
36 | /** |
||
37 | * The {@link View} used by this controller |
||
38 | * |
||
39 | * @var View |
||
40 | */ |
||
41 | protected $_view = null; |
||
42 | |||
43 | /** |
||
44 | * Constructs a controller, initializes the related view, |
||
45 | * and calls the controller's URI-indicated action method. |
||
46 | * |
||
47 | * @param string $viewClass |
||
48 | * View class. The default is <code>'View'</code>. |
||
49 | * @param string $template |
||
50 | * Resource path of the template for the view. The default |
||
51 | * is the empty string. |
||
52 | */ |
||
53 | protected function __construct($viewClass = 'View', |
||
54 | $template = null) |
||
55 | { |
||
56 | $this->_view = new $viewClass($template); |
||
57 | |||
58 | Application::getInstance()->setCurrentController($this); |
||
59 | |||
60 | $action = Application::getParam('action', $_REQUEST); |
||
61 | |||
62 | /* NOTE: No `==='; treat empty action like no specific action */ |
||
63 | if ($action == null) |
||
64 | { |
||
65 | $action = $this->_defaultAction; |
||
66 | } |
||
67 | |||
68 | $this->{lcfirst($action) . 'Action'}(); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Assigns a value to a template variable (after this, |
||
73 | * <var>$value</var> is available through |
||
74 | * <code>$this-><var>$name</var></code> in the view's template). |
||
75 | * <code>Controller</code>s should call this method instead of |
||
76 | * {@link View::assign()}. |
||
77 | * |
||
78 | * @param string $name |
||
79 | * Variable name |
||
80 | * @param mixed $value |
||
81 | * Variable value |
||
82 | * @param bool $encodeHTML |
||
83 | * If <code>true</code>, replace all potentially conflicting |
||
84 | * characters in <var>$value</var> with their HTML entity |
||
85 | * references. The default is <code>false</code>. |
||
86 | * @return mixed The assigned value (after possible HTML encoding) |
||
87 | * @see View::encodeHTML() |
||
88 | */ |
||
89 | protected function assign($name, $value, $encodeHTML = false) |
||
90 | { |
||
91 | return $this->_view->assign($name, $value, $encodeHTML); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Renders the {@link View} associated with this controller |
||
96 | * by including the <code>View</code>'s template. |
||
97 | * <code>Controller</code>s should call this method instead of |
||
98 | * <code>View::render()</code>. |
||
99 | * |
||
100 | * @param string $template |
||
101 | * Optional alternative template resource path. |
||
102 | * If not provided, the default template (the |
||
103 | * <code>View</code>'s <code>$template</code> property) |
||
104 | * will be used. |
||
105 | */ |
||
106 | public function render($template = null, $content = null) |
||
107 | { |
||
108 | $this->_view->render($template, $content); |
||
109 | } |
||
110 | } |
||
111 | |||
112 | ?> |