0

I am new for hooks in codeigniter. I have enabled the hooks in config file.

$config['enable_hooks'] = TRUE;

and then in hooks.php I have written my hook that is like below

$hook['pre_controller'] = array(
'class' => 'MyClass',
'function' => 'Myfunction',
'filename' => 'Myclass.php',
'filepath' => 'hooks',
'params' => '');

AND the class having function is as below

class MyClass {

function MyClass() {
    $this->CI = &get_instance();
    require_once(APPPATH . 'config/database.php');
}
function Myfunction() {
    $record = $this->CI->db->SELECT('*')
            ->FROM('currency')
            ->get()
            ->result();
    echo "<pre>";
    print_r($record);
    die;
}}

but i am getting a blank page. please tell me what is wrong with me.

  • Possible duplicate of [Reference - What does this error mean in PHP? – Nothing is seen. The page is empty and white.](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12772851#12772851) – CBroe Jan 14 '16 at 11:38
  • yes, I want to fetch the data from that table. What should i have to do.@CBroe – Ravi Prakash Awasthi Jan 14 '16 at 11:42
  • First of all, you need to find out what the reason for the “blank page” is. – CBroe Jan 14 '16 at 11:43

1 Answers1

0

I think your main problem was in this area.

function MyClass() {
    $this->CI = &get_instance();
    require_once(APPPATH . 'config/database.php');
}

Try

Note: codeigniter 3 versions are case sensitive should be first letter upper case only on class and file name.

application > hooks > My_class.php

<?php

class My_class {

public function __construct() {
    $this->CI = &get_instance();
    // Auto load database
    // require_once(APPPATH . 'config/database.php');
}


public function my_function() {
    $query = $this->CI->db->get('currency');

    $record = $query->result_array();

    echo "<pre>";
    print_r($record);
    echo "</pre>";

}

}

Config Hook

$hook['pre_controller'] = array(
'class' => 'My_class',
'function' => 'my_function',
'filename' => 'My_class.php',
'filepath' => 'hooks',
);

Autoload the database better option

$autoload['libraries'] = array('database');

Codeigniter Hooks