Welcome Guest, Not a member yet? Register   Sign In
hoooks ??[Solved]
#1

[eluser]egoslip[/eluser]
Hey guys I like to understand more about hooks in CI but I cant seem to wrap my head around it with just the user doc and the php.net doc file.

I basically want to do this
Code:
add_hook_function('hook_name',function_name());

function function_name(){
echo '<br> hey this is a function called by a hook <br>';
}

any other page or class or anything out side of the first file where i add the hook

Code:
function index(){
echo 'this is text before the hook called';
call_hook_function('hook_name');
echo 'this is text after the hook called';
}


I don't know how to do this with out including every file i use the add_hook_function .... I want it to be modules im using matchbox for the module system.

I basically want to add a module that has a hook then have it be able to add functions to any other loaded module calling that hook automatically.

Any help would be great
#2

[eluser]gigas10[/eluser]
Sorry if i'm wrong, isn't there a file named hooks.php in the system/application/config folder that you can modify to load hooks automatically, then use them from there. Hooks just allow you to modify the core of the framework without modifying the core of the framework... if you get my meaning. It allows you to add functions that get loaded into the framework during the initialization of the framework, if you read on the bottom of the hooks user guide, it gives you how to initialize the hook, and wear during the start up of the framework process the hook can be loaded.
#3

[eluser]egoslip[/eluser]
Ahh I see I think im getting it... you add your hooks to the hooks.php file in the config.

so if its a module then you would add a hooks.php to your modules config folder..

I now get that but.

i only want to execute the hooks at a certain function at a certain time and not at the framework.

like I want to make a hook then this hook will only execute in lets say the "test" function but only when its called inside the test function....

like can have this set up

class Test extends controller{
function index(){
call_hook('start');
}
}
then it would look for the hook named start. and run the code for it. before moving on.

lets say I have a module called settings then i want it to load all the settings for every module that is loaded. so i add a hook for every moduled and call it hook['setting_load_fields']; this would run the function that would generate the field for that module. I then can just use call_hook('setting_load_fields'); and each module that needs to load any settings will show up once the field is called. and if I remove the module then the field is no longer displaying in the settings module.

I don't know if I made it clear let me know if its still confusing as it hard to explain something I'm trying to do with out exact knowledge of how to do it.
#4

[eluser]Chad Fulton[/eluser]
I think you have misunderstood the nature of hooks in CodeIgniter:



1. There are no functions named add_hook_function(), call_hook_function(), or call_hook(). Are these functions you've created in your application?



2. [quote author="egoslip" date="1247193204"]Ahh I see I think im getting it... you add your hooks to the hooks.php file in the config.

so if its a module then you would add a hooks.php to your modules config folder..
[/quote]

What do you mean? There is no modules config folder, and there are no modules in CodeIgniter. Do you mean controllers? Either way, all hooks are defined in the system/application/config/hooks.php file. They cannot be defined anywhere else.



3. [quote author="egoslip" date="1247193204"]i only want to execute the hooks at a certain function at a certain time and not at the framework.

like I want to make a hook then this hook will only execute in lets say the "test" function but only when its called inside the test function....

like can have this set up

class Test extends controller{
function index(){
call_hook('start');
}
}
then it would look for the hook named start. and run the code for it. before moving on.[/quote]

This doesn't make any sense in the context of hooks. A hook is meant to allow you to execute arbitrary code at points in the CodeIgniter application flow where you would not normally be able to. If you just want to call a function from within your controllers, you do not need to call a hook (and in fact, you cannot call a hook).

I think you may be misusing the term hook. It sounds like you just want a function which will run some kind of standard procedure for your methods in your controllers. If that is the case, you could either extend the Controller class with a MY_Controller class and put the function in there, or you could create a helper.



4. [quote author="egoslip" date="1247193204"]lets say I have a module called settings then i want it to load all the settings for every module that is loaded. so i add a hook for every moduled and call it hook['setting_load_fields']; this would run the function that would generate the field for that module. I then can just use call_hook('setting_load_fields'); and each module that needs to load any settings will show up once the field is called. and if I remove the module then the field is no longer displaying in the settings module.[/quote]

I have no idea what you're talking about here.
#5

[eluser]egoslip[/eluser]
Thanks guys for your feedback. I figured the hook system had its limitations to those 8 point only.

here is what i was looking for it seems someone else wanted to accomplish the same thing i did and they where kind enough to make the fix public

http://ellislab.com/forums/viewthread/87350/

thanks Xeoncross author of the library
#6

[eluser]E1M2[/eluser]
Also checkout Neophyte's KhEvent library. I think it might do more of what your looking for... when building a plugin architecture.
http://ellislab.com/forums/viewthread/69753/




Theme © iAndrew 2016 - Forum software by © MyBB