Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter MY_Controller: is it only possible to extend core once?

I have successfully extended the core using the MY_Controller as described in CI's documentation.

This way I can put some repetitive functions (ie, auth check) in the constructor of MY_Controller so they always run before methods from my other controllers.

My issue now is that some parts of my webapp are open (ie, don't require login) and others require login.

Therefore, I cannot extend ALL my controllers from MY_Controller (which contains an auth check function).

I wondered if it would be possible to extend the core so as to have, say, LOG_Controller and NOLOG_Controller.

Then, controllers that require login would extend from LOG_Controller --- and controllers that do not require login would extend from NOLOG_Controller.

Is this posssible? (or is it bad form?)

It seems config/config.php only allows for one core extension prefix so I'm not sure it's possible.

Let me know what you think or if there's a better way of doing this. Thanks.

like image 962
pepe Avatar asked Nov 29 '22 15:11

pepe


2 Answers

Have a look at this article http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY - It is exactly what you are looking for.

like image 61
John Avatar answered Dec 04 '22 10:12

John


I use the following trick. Define all your base controllers in My_Controller.php:

<?php

class My_Controller extends CI_Controller{

}

class LOG_Controller extends My_Controller{

} 

class NOLOG_Controller extends My_Controller{

}
?>

This makes all defined controllers available later in your controllers. Just avoid making the base controllers too fat.

Also you can check this example: https://github.com/aquilax/novigeroi2/blob/master/application/core/AQX_Controller.php

like image 33
AquilaX Avatar answered Dec 04 '22 08:12

AquilaX