Welcome Guest, Not a member yet? Register   Sign In
Finding if method exists in current controller and running it?
#1

[eluser]Unknown[/eluser]
Hi!

I'm trying to use a helper to find if a method exists in the current controller in use and then call that function. Basically it should be similar to how the Form validation library handles callback functions, being able to find them in the controller or helper etc.

So far I've got this. If I just have the $ci-> stuff replaced with $this->ci->$handler it works fine if the function is in the controller itself, but NOT when it's in the helper.

This is in controller:
Code:
$handlers = array('example1','example2'); //Functions to run
$input = $this->input->post('field');
$output = run_handlers($handlers,$input);
This is in a helper:
Code:
function run_handlers($handlers,$input)
{
  $ci =& get_instance();

  foreach($handlers as $handler)
  {
   if(method_exists($ci->router->class,$handler)) //Finds method ok here
   {
    $input = $ci->$handler($input); //But how do I run it here?
   }
   else if(function_exists($handler))
   {
    $input = $handler($input);
   }
   else
   {
    trigger_error("Handler method or function not found", E_USER_WARNING);
    break;
   }
  }
  return $input;
}




Theme © iAndrew 2016 - Forum software by © MyBB