在Codeigniter中手动调用/调用钩子 [英] Calling/invoking a hook manually in Codeigniter
问题描述
我在网络上搜索了手动呼叫/调用钩子和类似内容,但找不到任何东西。 Codeigniter中有这样的事情吗?我有一个钩子,下面有一个钩子,它会按预期触发,以防万一,如果没有,我想在代码中手动调用它。
I searched for calling/invoking a hook manually and similar stuff on the web but couldn't find anything. Is there a such thing in codeigniter? I have a hook below which gets triggered as expected but just in case if doesn't, then I want to invoke it manually in my code.
谢谢
$hook['post_controller_constructor'] [] =
array(
'class' => 'load_designs',
'function' => 'do_load',
'filename' => 'load_designs_hook.php',
'filepath' => 'hooks',
'params' => ''
);
推荐答案
要调用钩子,您可以加载 Hooks
核心类,并通过 _call_hook()
方法调用该hook,如下所示:
In order to call a hook, you could load the Hooks
core class and call the hook by _call_hook()
method as follows:
在您的控制器中:
$hook =& load_class('Hooks', 'core');
$hook->_call_hook('post_controller_constructor');
但是,如果您需要调用钩子类的特定方法,则应该手动进行:
However, if you need to call a specific method of a hook class, you should do that manually:
if (! file_exists($file_path = APPPATH . 'hooks/MyClass.php'))
{
exit('The hook file does not exist.');
}
// load the hook file.
require $file_path;
$hook = new MyClass();
$hook->Myfunction(array('Hello', 'World!'));
如果需要,您还可以创建一个辅助函数来执行上述逻辑。
You can also make a helper function to do the above logic if needed.
这篇关于在Codeigniter中手动调用/调用钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!