![]() |
method that loads before controller modules - 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: method that loads before controller modules (/showthread.php?tid=26340) |
method that loads before controller modules - El Forum - 01-11-2010 [eluser]pocketmax[/eluser] I have a controller with a bunch of modules in it and I can reduce a lot of duplicate code if there was a central place that I could put a piece of code that would load before my modules do. Is there a way to put code in a method that would run before any of my modules run? So if someone goes to myurl.com/blog/update/ and I in my code... Code: class Blog extends Controller { method that loads before controller modules - El Forum - 01-11-2010 [eluser]WebsiteDuck[/eluser] Code: class Blog extends Controller { method that loads before controller modules - El Forum - 01-11-2010 [eluser]pocketmax[/eluser] What I meant was something CI specific. for example, cake has the _beforeScaffold method. But if using the construct is the only/best way of doing it, I'm down for it. Thanks method that loads before controller modules - El Forum - 01-11-2010 [eluser]WebsiteDuck[/eluser] Sorry I misunderstood, maybe hooks is what you're looking for? http://ellislab.com/codeigniter/user-guide/general/hooks.html method that loads before controller modules - El Forum - 01-11-2010 [eluser]pocketmax[/eluser] Yeah, I read that and I'm a little confused. From a first glance, it looks really powerful but it's example doesn't make sense to me... Code: $hook['pre_controller'][] = array( It doesn't say whether or not the classes and functions are custom, pre-exist have to be created or are already created. Heres a better example of what I'm talking about... I have a controller named Blog with a module named update and I want a hook to fire before that controller runs, so I would do this? Code: $hook['pre_controller'][] = array( So with that code, I'm telling it what controller, what module, where the file is that has the hook, the filepath to the hook and the default params I want to pass into the hook? I know they give a definition of what each entry means but it's a little confusing to me. Also, it doesn't give an example of the actual contents of the hook it's self. Is the hook a class and CI creates an object from it on the fly to glue it to my controller? is it an object I instantiate myself from a class I wrote or is it a simple function? And if it's a function, won't I loose scope since functions don't get $this? So won't that limit the abilities of my hook? method that loads before controller modules - El Forum - 01-11-2010 [eluser]jedd[/eluser] [quote author="pocketmax" date="1263267431"] I have a controller with a bunch of modules in it and I can reduce a lot of duplicate code if there was a central place that I could put a piece of code that would load before my modules do. [/quote] This kind of thing is covered in [url="/wiki/FAQ"]the FAQ[/url] I think. method that loads before controller modules - El Forum - 01-11-2010 [eluser]Dyllon[/eluser] [quote author="pocketmax" date="1263267431"]I have a controller with a bunch of modules in it and I can reduce a lot of duplicate code if there was a central place that I could put a piece of code that would load before my modules do. Is there a way to put code in a method that would run before any of my modules run? So if someone goes to myurl.com/blog/update/ and I in my code... Code: class Blog extends Controller { I'm guessing by modules you mean methods in which case you can put $this->foo='bar'; in the constructor method that loads before controller modules - El Forum - 01-11-2010 [eluser]pocketmax[/eluser] I thought when classes are used as controllers, the methods are suppose to be called modules? If not, correct me now so I get it right later. method that loads before controller modules - El Forum - 01-11-2010 [eluser]Dyllon[/eluser] Modules are a group of components (models, controllers, views, etc.) and are not natively supported by CI. The functions in a controller are referred to as methods method that loads before controller modules - El Forum - 01-12-2010 [eluser]laytone[/eluser] A module is a part of a component which is a part of a system and really the definition of a module has nothing to do with anything. In joomla a module is one part of a page. Define Module: # a self-contained component (unit or item) that is used in combination with other components or # An independent part of a program. Much of Apache's functionality is contained in modules that you can choose to include or exclude. ... Definitely not another way to say 'method' or 'function'! Use your constructor and / or extend your controller class to solve your problem.. Constructors a specifically used to call function (aka methods) before any other function in the class are called. use constructors to load common variables and manipulate data that need to be access-able from any part of your class / controller. |