-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathMY_Controller.php
More file actions
201 lines (173 loc) · 5.76 KB
/
Copy pathMY_Controller.php
File metadata and controls
201 lines (173 loc) · 5.76 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* A base controller for CodeIgniter with view autoloading, layout support,
* model loading, helper loading, asides/partials and per-controller 404
*
* @link http://github.com/jamierumbelow/codeigniter-base-controller
* @copyright Copyright (c) 2012, Jamie Rumbelow <http://jamierumbelow.net>
*/
class MY_Controller extends CI_Controller
{
/* --------------------------------------------------------------
* VARIABLES
* ------------------------------------------------------------ */
/**
* The current request's view. Automatically guessed
* from the name of the controller and action
*/
protected $view = '';
/**
* An array of variables to be passed through to the
* view, layout and any asides
*/
protected $data = array();
/**
* The name of the layout to wrap around the view.
*/
protected $layout;
/**
* An arbitrary list of asides/partials to be loaded into
* the layout. The key is the declared name, the value the file
*/
protected $asides = array();
/**
* A list of models to be autoloaded
*/
protected $models = array();
/**
* A formatting string for the model autoloading feature.
* The percent symbol (%) will be replaced with the model name.
*/
protected $model_string = '%_model';
/**
* A list of helpers to be autoloaded
*/
protected $helpers = array();
/* --------------------------------------------------------------
* GENERIC METHODS
* ------------------------------------------------------------ */
/**
* Initialise the controller, tie into the CodeIgniter superobject
* and try to autoload the models and helpers
*/
public function __construct()
{
parent::__construct();
$this->_load_models();
$this->_load_helpers();
}
/* --------------------------------------------------------------
* VIEW RENDERING
* ------------------------------------------------------------ */
/**
* Override CodeIgniter's despatch mechanism and route the request
* through to the appropriate action. Support custom 404 methods and
* autoload the view into the layout.
*/
public function _remap($method)
{
if (method_exists($this, $method))
{
call_user_func_array(array($this, $method), array_slice($this->uri->rsegments, 2));
}
else
{
if (method_exists($this, '_404'))
{
call_user_func_array(array($this, '_404'), array($method));
}
else
{
show_404(strtolower(get_class($this)).'/'.$method);
}
}
$this->_load_view();
}
/**
* Automatically load the view, allowing the developer to override if
* he or she wishes, otherwise being conventional.
*/
protected function _load_view()
{
// If $this->view == FALSE, we don't want to load anything
if ($this->view !== FALSE)
{
// If $this->view isn't empty, load it. If it isn't, try and guess based on the controller and action name
$view = (!empty($this->view)) ? $this->view : $this->router->directory . $this->router->class . '/' . $this->router->method;
// Load the view into $yield
$data['yield'] = $this->load->view($view, $this->data, TRUE);
// Do we have any asides? Load them.
if (!empty($this->asides))
{
foreach ($this->asides as $name => $file)
{
$data['yield_'.$name] = $this->load->view($file, $this->data, TRUE);
}
}
// Load in our existing data with the asides and view
$data = array_merge($this->data, $data);
$layout = FALSE;
// If we didn't specify the layout, try to guess it
if (!isset($this->layout))
{
if (file_exists(APPPATH . 'views/layouts/' . $this->router->class . '.php'))
{
$layout = 'layouts/' . $this->router->class;
}
else
{
$layout = 'layouts/application';
}
}
// If we did, use it
else if ($this->layout !== FALSE)
{
$layout = $this->layout;
}
// If $layout is FALSE, we're not interested in loading a layout, so output the view directly
if ($layout == FALSE)
{
$this->output->set_output($data['yield']);
}
// Otherwise? Load away :)
else
{
$this->load->view($layout, $data);
}
}
}
/* --------------------------------------------------------------
* MODEL LOADING
* ------------------------------------------------------------ */
/**
* Load models based on the $this->models array
*/
private function _load_models()
{
foreach ($this->models as $model)
{
$this->load->model($this->_model_name($model), $model);
}
}
/**
* Returns the loadable model name based on
* the model formatting string
*/
protected function _model_name($model)
{
return str_replace('%', $model, $this->model_string);
}
/* --------------------------------------------------------------
* HELPER LOADING
* ------------------------------------------------------------ */
/**
* Load helpers based on the $this->helpers array
*/
private function _load_helpers()
{
foreach ($this->helpers as $helper)
{
$this->load->helper($helper);
}
}
}