CodeIgniter Forums
Hooks Question - 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: Hooks Question (/showthread.php?tid=2173)



Hooks Question - El Forum - 07-19-2007

[eluser]Marcus Cavalcanti[/eluser]
Hooks is a very helpful feature of CI, but i have one question about Hooks.

Exists a way to define the specific controllers/methods of a hook?

Example:
Code:
$hook['pre_controller'] = array(
        'class'     => 'MyClass',
        'function'     => 'Myfunction',
        'filename'     => 'Myclass.php',
        'filepath'     => 'hooks',
        'params'    => array('beer', 'wine', 'snacks')
        'controller_methods' => array('user/add' , 'blog/views' , 'pool/vote')
        );

In the example below the hook works only for controllers/methods in "controller_methods" attributes. It is like a filter works, but filter is "deprecated".

Thanks


Hooks Question - El Forum - 07-20-2007

[eluser]Glen Swinfield[/eluser]
If what you are asking is can you set a pre-controller hook to run only when a specific controller/method is called, I'm pretty sure the answer is no. But as a quick fix you could just:

Code:
$uri_array = array('user/add' , 'blog/views' , 'pool/vote');
if(in_array($_SERVER['REQUEST_URI'], $uri_array){
   $hook['pre_controller'] = array(
  'class'  => 'MyClass',
  'function'  => 'Myfunction',
  'filename'  => 'Myclass.php',
  'filepath'  => 'hooks',
  'params' => array('beer', 'wine', 'snacks')
   );
}

This would need some tweaking to allow for tailing slashes etc but you get the idea.