forked from jamierumbelow/codeigniter-base-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMY_Controller.php
More file actions
295 lines (257 loc) · 8.54 KB
/
Copy pathMY_Controller.php
File metadata and controls
295 lines (257 loc) · 8.54 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* 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
* @author Jamie Rumbelow <http://jamierumbelow.net>
* @modified Justin Busschau <http://bythepeople.co.uk>
* @copyright Copyright (c) 2012, Jamie Rumbelow <http://jamierumbelow.net>
*/
class MY_Controller extends CI_Controller
{
/* --------------------------------------------------------------
* VARIABLES
* ------------------------------------------------------------ */
/**
* Define a default action for this controller if index()
* is not available
*/
protected $index = '';
/**
* 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 to be loaded into
* the layout. The key is the declared name, the value the file
*/
protected $asides = array();
/**
* An arbitrary list of partials to be included into
* the layout. The key is the declared name, the value the content
*/
protected $partials = 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';
/**
* Whether or not to use the CI Parser when loading views
*/
protected $use_parser = FALSE;
/**
* Load controller-specific language files.
* If set to FALSE, nothing is loaded
* Otherwise load the lang file(s) indicated
*/
protected $translate = TRUE;
/*
* 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_translation();
$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 (!empty($this->index) && method_exists($this, $this->index))
{
$this->router->method = $this->index;
call_user_func_array(array($this, $this->index), array_slice($this->uri->rsegments, 2));
}
elseif (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 is empty, try and guess based on the controller and action name
$view = (!empty($this->view)) ? $this->view : (($this->router->fetch_module()) ? $this->router->class . '/' . $this->router->method : $this->router->directory . $this->router->class . '/' . $this->router->method);
// Load the view into $yield
$data['yield'] = $this->_load($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($file, $this->data, TRUE);
}
}
// Do we have any partials? Add them.
if (!empty($this->partials))
{
foreach ($this->partials as $name => $content)
{
$data['yield_'.$name] = $content;
}
}
// 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($layout, $data);
}
}
}
/**
* Load the view, or parse the template ...
*/
protected function _load($view, $data, $return = FALSE)
{
if ($this->use_parser)
{
return $this->parser->parse($view, $data, $return);
}
else
{
return $this->load->view($view, $data, $return);
}
}
/* --------------------------------------------------------------
* MODEL LOADING
* ------------------------------------------------------------ */
/**
* Load models based on the $this->models array
*/
private function _load_models()
{
foreach ($this->models as $model)
{
$alias = ($m = strpos($model, '/')) ? substr($model, $m+1) : $model;
$this->load->model($this->_model_name($model), $alias);
}
}
/**
* Returns the loadable model name based on
* the model formatting string
*/
protected function _model_name($model)
{
return str_replace('%', $model, $this->model_string);
}
/* --------------------------------------------------------------
* TRANSLATION
* ------------------------------------------------------------ */
/**
* Load lang files based on the $this->translate variable
* This function relies on the default language set in your
* application/config/config.php file or set programatically
* to allow for run-time language changes
*/
private function _load_translation()
{
// if $this->translate is FALSE we don't load anything
if ($this->translate !== FALSE)
{
if (is_array($this->translate))
{
foreach ($this->translate as $lang)
{
$this->lang->load($lang);
}
}
// if $this->translate is true, we guess a translation filename
elseif ($this->translate === TRUE)
{
$this->lang->load($this->router->class);
}
// otherwise we assume a filename is given and load that
else
{
$this->lang->load($this->translate);
}
}
}
/* --------------------------------------------------------------
* HELPER LOADING
* ------------------------------------------------------------ */
/**
* Load helpers based on the $this->helpers array
*/
private function _load_helpers()
{
foreach ($this->helpers as $helper)
{
$this->load->helper($helper);
}
}
}