Search code examples
phpcodeignitercodeigniter-2

passing data from hook to view in codeigniter


can i passing data from hook to view, If is it possible please explain.

for example

 $hook['post_controller_constructor'][] = array(
    'class'    => 'Varify_user',
    'function' => 'user_project',
    'filename' => 'varify_project.php',
    'filepath' => 'hooks',
    'params'   => array('')
);

i want send some array data varify_project.php(hook file) to view.


Solution

  • I do so

    application/core/MY_Loader.php

    class MY_Loader extends CI_Loader {
        static $add_data = array();
        public function view($view, $vars = array(), $return = FALSE)
        {
           self::$add_data = array_merge($vars, self::$add_data);
           return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array(self::$add_data), '_ci_return' => $return));
        }
    }
    

    application/config/hooks.php

    $hook['post_controller_constructor'] = function() {
        MY_Loader::$add_data['hello'] = "Hello World";
    } ;