Preventing DRY in OOP |
[eluser]behnampmdg3[/eluser]
I notice I have this in some of my controllers. Exact same code. How can I move this somewhere else and still be able to simply use the properties (variables). Thanks Code: protected $logged;
[eluser]ivantcholakov[/eluser]
Make a library or a model and the use it on controllers.
[eluser]CroNiX[/eluser]
Use a base controller, and have all of your other controllers that need this functionality extend that base controller. http://ellislab.com/forums/viewthread/241147/ If you need multiple base controllers (maybe one for front end, a different one for backend), then this article would help: http://philsturgeon.co.uk/blog/2010/02/C...ing-it-DRY
[eluser]behnampmdg3[/eluser]
Didn't work; I get Quote:Fatal error: Class 'My_Logged_Class' not foundSaved this as My_Logged_Class.php in application/core Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Code: class Member_details extends My_Logged_Class { Um, not sure what to do.
[eluser]behnampmdg3[/eluser]
Do I have to autoload that controller somewhere? If so how should I autoload a controller? Thanks
[eluser]CroNiX[/eluser]
No, it gets autoloaded by the framework when it calls the native class. If you follow the first link I posted exactly, it will work. CI will load the normal object, like CI_Controller. Then it automatically checks to see if it was extended with MY_Controller. If it exists, it loads that. Then your individual controller class, like "logged_class". You ONLY use the MY_ prefix when you extend a CI NATIVE CLASS, not your own. And it must be named exactly the same as its counterpart. For example, you put: Code: class My_Logged_Class extends CI_Controller { when it needs to be (including exact capitalization) Code: class MY_Controller extends CI_Controller And then YOUR controllers extend MY_Controller instead of CI_Controller like normal (but don't use MY_ for the name of YOUR controller!!) Code: class Logged_Class extends MY_Controller { See these userguide pages for more info: http://ellislab.com/codeigniter/user-gui...asses.html http://ellislab.com/codeigniter/user-gui...aries.html
[eluser]InsiteFX[/eluser]
You can name your Base_Controller anything you want but it must be saved in ./application/core as MY_Controller.php Saveas: ./application/core/MY_Controller.php Code: class Base_Controller extends CI_Controller Then when creating new controllers extend them from Base_Controller not CI_Controller
[eluser]behnampmdg3[/eluser]
Thanks. It's working. The reason it was NOT working is that my computer strangely caches a lot of stuff! Anyways, it is working now. Yest another question. I still repeat some code in most controllers: Code: $data['page_title'] = "My Account - "; How can I move them to somewhere so in case I need to chage I won't have to change them in every single controller? Thanks
[eluser]InsiteFX[/eluser]
Move them to the Base_Controller Code: public static $page_data = array();
|
Welcome Guest, Not a member yet? Register Sign In |