Repeating piece of code - 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: Repeating piece of code (/showthread.php?tid=9994) |
Repeating piece of code - El Forum - 07-15-2008 [eluser]lopetzi[/eluser] Hello world, I am new to CI, but i really like it. I'm having one small problem: I have a piece of code that i will have to repeat in almost every controller. Is there any way to put that code in a file and execute it... ? home.php Code: class Home extends Controller { I've tried to copy-paste that code into a library and then load the library, but i get an error... How do you handle the code that repeats in your controllers ? Thanks, George Repeating piece of code - El Forum - 07-15-2008 [eluser]TheLoops[/eluser] Just subclass "Controller", add the function to it and then use: Code: class Home extends MySubclassedController { Repeating piece of code - El Forum - 07-15-2008 [eluser]lopetzi[/eluser] I made a MY_Controller library: Code: <?php and now home looks like this: Code: <?php I get errors that variables in the library are not set. I don't know how to use the data in the library For example: how do i echo $user from the library? Thanks Repeating piece of code - El Forum - 07-15-2008 [eluser]loathsome[/eluser] Did you call the auto function? Use the parent keyword to do so. In my current project, some of my controllers look like this: Code: class Blah extends BaseController { And the BaseController looks something like this: Code: class BaseController extends Controller { Best of luck. Repeating piece of code - El Forum - 07-16-2008 [eluser]lopetzi[/eluser] I was doing it right, but i didn't know how to fetch data from my controller... Thanks |