Acces controller function from library / alternative work around? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Acces controller function from library / alternative work around? (/showthread.php?tid=37822) |
Acces controller function from library / alternative work around? - El Forum - 01-21-2011 [eluser]Bramme[/eluser] Hey everybody Here's my issue: I'm developping a CRUD application that generates standard HTML tables and forms for my database tables. I've extended the CI_Controller with MY_Controller and have written a few basic methods (index(), edit()...) that generate default html. For every table in my database, I then create a controller that extends from MY_Controller. This ensures every controller, extended from MY_Controller has the same pages (index, edit). If I need to display something other than the default HTML, I just overwrite the index/edit function in my table controller. The index page of MY_Controller produces an HTML table with all the fields in the database and displays those fields as flat text. The function that generates the html table, is in a library called Crud. So in crud.php I do this at a certain point: Code: foreach ($query->result() as $temp_row) Code: foreach ($query->result() as $temp_row) Code: function _format_table_field($field, $datarow) Does anybody have a suggestion as to how to solve this? Acces controller function from library / alternative work around? - El Forum - 01-21-2011 [eluser]Adam K[/eluser] Hi Bramme, as I mentioned on twitter, I'm not sure about three things: - $this->CI. what is it? Is it CI via get_instance(), or just something you thought would work? If the latter, use code from here to get to CI instance in library CI object from library - also, I'm not sure whether 'core' controller class is initiaded (maybe only extended), so you have to initiate it. - I'm not sure whethere you can call underscored functions from outside Adam Acces controller function from library / alternative work around? - El Forum - 01-21-2011 [eluser]Bramme[/eluser] 1. $this->CI is indeed a variable filled with $this->CI =& get_instance() 2. When I $this->CI->load->library('MY_Controller') (or CI_Controller or Controller) I get a "Unable to load the requested class: my_controller", which seems logical as you otherwise never have to load it. 3. Scrapping the underscore didn't change anything. Acces controller function from library / alternative work around? - El Forum - 01-21-2011 [eluser]Adam K[/eluser] Actually, I was thinking, why call it from core class? I'm bit lost here about your files structure, but: a. If you're calling the underscore function from controller, why not do $this->_function or (if it's from diferent controller) $this->that_loaded_controller->_function() b. If it's called from library, I think there must be a way to run that function in original caller Controller: controllerA->load library->call function X with parameter $this in functionX use $param->_function() Or, am I that wrong (I haven't worked with CI longer time) Acces controller function from library / alternative work around? - El Forum - 01-21-2011 [eluser]Bramme[/eluser] I have update the initial post a bit, I hope this makes things more clear. Acces controller function from library / alternative work around? - El Forum - 01-22-2011 [eluser]Bramme[/eluser] I'm going to bump this, because I really need it solved. Let me simplify my question: can I access controller methods in a library? |