Including functions |
[eluser]jwag[/eluser]
Hello, I have a few functions that I need to use in a bunch of classes. They need to be part of the class because, for example, one of them is a check_session() that makes sure the user is logged in and has access to what they're trying to get to, and uses $this->session->userdata(). So right now, just for development purposes, I have - Controller class -- Admin class extends Controller -- Survey class extends Controller -- Exam class extends Controller ... etc. Would the best way to get these functions accessable to all the other classes have a class between the Controller class and the others in the hierarchy? e.g. - Controller class -- MyClass class extends Controller --- Admin class extends MyClass --- Survey class extends MyClass --- Exam class extends MyClass ... etc. ? And also, how would I load, for example, the MyClass class? Stick a require_once(myclass.php) at the top of all of the other (Admin, Survey, Exam, etc.) classes? I did see in the User Guide the directions for creating your own libraries. However, while I am fairly experienced with PHP, I'm not so much with the OOP/classes in PHP, and I'm not sure if those classes loaded from my libraries would be able to access everything else (e.g. $this->session->userdata()). Thanks for any help/advice you can provide.
[eluser]Armchair Samurai[/eluser]
Create a library called MY_Controller (as per the user guide - Extending Native Libraries) and extend MyClass from there. After than, you can extend your controllers from MyClass. Code: /* From MY_Controller.php */
[eluser]ApranaxFortress[/eluser]
@Armchair Samurai If i will do this structural can i call other controllers functions ? (like wick http://codeigniter.com/wiki/Wick/) Lets say i have a two controllers Blogs and Comments Can i call like this in my Blog controller: $this->comments->list_comments($id)
[eluser]jwag[/eluser]
Ok, now how do I get the new one I created to load. I named mine Basecontroller. (NOTE: I set the prefix from MY_ to LIB_ on system/application/config/config.php) In system/application/libraries/LIB_Basecontroller, I have my class, which starts with... Code: class Basecontroller extends Controller { Then, in system/application/controllers/admin/admin.php, I put... Code: class Admin extends BaseController { I edited system/application/config/autoload.php and tried to add the library as both LIB_Basecontroller, Basecontroller, LIB_basecontroller, basecontroller, etc., and all give me the error message: Fatal error: Class 'Basecontroller' not found in /home/leaf/webdocs/system/application/controllers/admin/admin.php on line 3 What exactly do I need to put, and where, to load the Basecontroller? Thanks again for any help/direction.
[eluser]jwag[/eluser]
Actually, I figured it out. I was actually breaking it by putting it in the system/application/config/autoload.php file. Removing it actually fixed it (although I'm confused as to why it did -- that didn't seem to by what the PHP error in my previous post was saying). Oh well, it's working. :o) |
Welcome Guest, Not a member yet? Register Sign In |