5

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.

Pavnish Yadav
  • 2,728
  • 3
  • 15
  • 14

3 Answers3

3

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.

Ben Broadley
  • 632
  • 6
  • 14
1

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";
} ;
halfer
  • 19,824
  • 17
  • 99
  • 186
splash58
  • 26,043
  • 3
  • 22
  • 34
  • 1
    Consider editing your answer to add an explanation to why and how your code solves the problem. – Mephy Apr 18 '15 at 13:50
  • 1
    I agree with @Mephy, and the same feedback has been offered on your most recent contribution. Since no edit has been made on this question, I am downvoting, with apologies. If you would be willing to add a sentence or two, I will gladly remove the DV. – halfer Apr 27 '15 at 23:21
  • I am realy don't understand, what could be added to these some strings of simple code. it, seems, is clear for those, who works with Codeigniter's hooks – splash58 Apr 28 '15 at 09:24
1

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));
    }
}