1

I've defined a hook in config/hooks.php something like below:

$hook['post_controller'][] = 
    array(
        'class'    => 'notify',
        'function' => 'sendEmail',
        'filename' => 'notify.php',
        'filepath' => 'controllers'
    );

Now this hook function will run after each controller's method executed. And it's running the way I want it to run. So upto this everything is fine.

Now let say I have a controller called dashboard and a method in it called index. In the index method I'm calling different model method and fetching data and storing it with different variables.

Now I want to use those variable in my hook notify-sendEmail method.

I'm not sure if this is even possible or not. If it is possible and anyone has done that kind of functionality please help me!

Basically my question is : Can we access all object data from current controller method in hook controller method.

Deepak Biswal
  • 4,280
  • 2
  • 20
  • 37
  • This link might help http://stackoverflow.com/questions/29715760/passing-data-from-hook-to-view-in-codeigniter –  Dec 01 '15 at 10:52
  • With `params` we can pass an array to view? But how can I get `dashboard ->index` method's variables in hooks.php to assign it to `params`? – Deepak Biswal Dec 01 '15 at 11:09

1 Answers1

0

you can use array to pass your data into your hook. for example: in your index method on dashboard class, userdata already store in $userdata:

// create property for userdata into big object
$this->userdata = $userdata;

then in your hook notify. you need to check the userdata property is exist or not?

function sendEmail(){
    $CI =& get_instance();
    $required = array('email', 'subject', 'body'); // asume these parameter required

    // required userdata property. if property not set then print error
    if (!property_exists($CI, "userdata"))
    {
        echo "userdata required";
        return; // do nothing.
    }

    // if userdata is not array then print error
    if (!is_array($CI->userdata))
    {
        echo "invalid format userdata";
        return; // do nothing
    }

    // still here? it means userdata property exist and it's array
    $config_email = array();
    foreach($required as $key => $value)
    {
       if (!array_key_exist($value, $CI->userdata))
       {
            // some required parameter not exist.
            echo "field ".$value." is required";
            return;
       }

       // passing data into $config_email
       $config_email[$value] = $CI->userdata[$value];
    }

   //`do your stuff here to send email..
   // $config_email already set userdata. you still can modified them

}
Ari Djemana
  • 1,229
  • 12
  • 12