Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 362
Pavnish Yadav Avatar asked Apr 18 '15 10:04

Pavnish Yadav


3 Answers

If you are wanting to add additional data at the time of loading the view, you could extend the core loader class like this:

application/core/MY_Loader.php

<?php
class MY_Loader extends CI_Loader {
    public function view($view, $vars = array(), $return = FALSE)
    {
        $vars['hello'] = "Hello World";
        return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
    }
}

the $vars['hello'] would then create a variable that you can use in any view called $hello and could be repeated to create any number of variables providing that you wanted them to be used on every page in your application.

like image 120
Ben Broadley Avatar answered Oct 17 '22 02:10

Ben Broadley


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";
} ;
like image 29
splash58 Avatar answered Oct 17 '22 01:10

splash58


I don't have enough rep to comment splash58's answer so I'm adding this here in case it is useful to someone.

Due to _ci_object_to_array() not being available anymore and sending an error the custom loader code should be (as it is in core since 3.1.3) :

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_prepare_view_vars(self::$add_data), '_ci_return' => $return));
    }
}
like image 1
LeprechaunCode Avatar answered Oct 17 '22 01:10

LeprechaunCode