1 | <?php namespace Myth\Controllers; |
||
88 | class BaseController extends \CI_Controller { |
||
89 | /** |
||
90 | * The type of caching to use. The default values are |
||
91 | * set globally in the environment's start file, but |
||
92 | * these will override if they are set. |
||
93 | */ |
||
94 | protected $cache_type = NULL; |
||
95 | protected $backup_cache = NULL; |
||
96 | |||
97 | // If set, this language file will automatically be loaded. |
||
98 | protected $language_file = NULL; |
||
99 | |||
100 | // If set, this model file will automatically be loaded. |
||
101 | protected $model_file = NULL; |
||
102 | |||
103 | //-------------------------------------------------------------------- |
||
104 | |||
105 | public function __construct() |
||
121 | |||
122 | //-------------------------------------------------------------------- |
||
123 | |||
124 | //-------------------------------------------------------------------- |
||
125 | // Setup Methods |
||
126 | //-------------------------------------------------------------------- |
||
127 | // These methods are used during the initial constructor, but split out |
||
128 | // here so that child controllers can easily override individual methods |
||
129 | // if they need to customize that aspect of the startup. |
||
130 | |||
131 | /** |
||
132 | * Gets the cache up and running. The site-wide cache settings can be |
||
133 | * set in the application config file. Each controller can override these |
||
134 | * settings using the 'cache_type' and 'backup_cache' class vars. |
||
135 | */ |
||
136 | protected function setupCache() |
||
153 | |||
154 | //-------------------------------------------------------------------- |
||
155 | |||
156 | /** |
||
157 | * Handles any autoloading of files, like language or model files, |
||
158 | * that can be used throughout the controller. |
||
159 | */ |
||
160 | protected function autoload() |
||
173 | |||
174 | //-------------------------------------------------------------------- |
||
175 | |||
176 | /** |
||
177 | * If settings allow, will auto-migrate the system to the latest |
||
178 | * available migrations. |
||
179 | */ |
||
180 | protected function autoMigrate() |
||
197 | |||
198 | //-------------------------------------------------------------------- |
||
199 | |||
200 | /** |
||
201 | * Handles setting up the profiler. |
||
202 | */ |
||
203 | protected function setupProfiler() |
||
213 | |||
214 | //-------------------------------------------------------------------- |
||
215 | |||
216 | |||
217 | //-------------------------------------------------------------------- |
||
218 | // Simple Rendering Methods |
||
219 | //-------------------------------------------------------------------- |
||
220 | |||
221 | /** |
||
222 | * Renders a string of aribritrary text. This is best used during an AJAX |
||
223 | * call or web service request that are expecting something other then |
||
224 | * proper HTML. |
||
225 | * |
||
226 | * @param string $text The text to render. |
||
227 | * @param bool $typography If TRUE, will run the text through 'Auto_typography' |
||
228 | * before outputting to the browser. |
||
229 | * |
||
230 | * @return void [type] [description] |
||
231 | */ |
||
232 | public function renderText( $text, $typography = FALSE ) |
||
248 | |||
249 | //-------------------------------------------------------------------- |
||
250 | |||
251 | /** |
||
252 | * Converts the provided array or object to JSON, sets the proper MIME type, |
||
253 | * and outputs the data. |
||
254 | * |
||
255 | * Do NOT do any further actions after calling this action. |
||
256 | * |
||
257 | * @param mixed $json The data to be converted to JSON. |
||
258 | * |
||
259 | * @throws RenderException |
||
260 | * @return void |
||
261 | */ |
||
262 | public function renderJSON( $json ) |
||
279 | |||
280 | //-------------------------------------------------------------------- |
||
281 | |||
282 | /** |
||
283 | * Sends the supplied string to the browser with a MIME type of text/javascript. |
||
284 | * |
||
285 | * Do NOT do any further processing after this command or you may receive a |
||
286 | * Headers already sent error. |
||
287 | * |
||
288 | * @param mixed $js The javascript to output. |
||
289 | * |
||
290 | * @throws RenderException |
||
291 | * @return void |
||
292 | */ |
||
293 | public function renderJS( $js = NULL ) |
||
304 | |||
305 | //-------------------------------------------------------------------- |
||
306 | |||
307 | /** |
||
308 | * Breaks us out of any output buffering so that any content echo'd out |
||
309 | * will echo out as it happens, instead of waiting for the end of all |
||
310 | * content to echo out. This is especially handy for long running |
||
311 | * scripts like might be involved in cron scripts. |
||
312 | * |
||
313 | * @return void |
||
314 | */ |
||
315 | public function renderRealtime() |
||
323 | |||
324 | //-------------------------------------------------------------------- |
||
325 | |||
326 | /** |
||
327 | * Integrates with the bootstrap-ajax javascript file to |
||
328 | * redirect the user to a new url. |
||
329 | * |
||
330 | * If the URL is a relative URL, it will be converted to a full URL for this site |
||
331 | * using site_url(). |
||
332 | * |
||
333 | * @param string $location [description] |
||
334 | */ |
||
335 | public function ajaxRedirect( $location = '' ) |
||
351 | |||
352 | //-------------------------------------------------------------------- |
||
353 | |||
354 | /** |
||
355 | * Attempts to get any information from php://input and return it |
||
356 | * as JSON data. This is useful when your javascript is sending JSON data |
||
357 | * to the application. |
||
358 | * |
||
359 | * @param strign $format The type of element to return, either 'object' or 'array' |
||
360 | * @param int $depth The number of levels deep to decode |
||
361 | * |
||
362 | * @return mixed The formatted JSON data, or NULL. |
||
363 | */ |
||
364 | public function getJSON( $format = 'object', $depth = 512 ) |
||
370 | |||
371 | //-------------------------------------------------------------------- |
||
372 | |||
373 | } |
||
374 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.