CodeIgniter Forums
A Few Hook Questions - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: A Few Hook Questions (/showthread.php?tid=60219)

Pages: 1 2


A Few Hook Questions - El Forum - 02-06-2014

[eluser]xtremer360[/eluser]
I'm trying to figure out a few things like:

1. If this is the properly written hook file.
2. I keep getting errors like these:

Code:
Severity: Notice
Message: Trying to get property of non-object
Filename: hooks/Sys_prescript.php
Line Number: 10

Fatal error: Call to a member function fetch_class() on a non-object in /public_html/application/hooks/Sys_prescript.php on line 10

Code:
<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class Sys_prescript {
    private $CI;
    function initial_run()
    {
        $this->CI =& get_instance();
        $public_access = array('login', 'registration');
        $current_class = $this -> CI -> router -> fetch_class();
        $session_data = $this -> session -> userdata('user_id');
        if ($session_data == FALSE)
        {
            if (!in_array($current_class, $public_access))
            {
                redirect('login', 'refresh');
            }
        }
        else
        {
        }
    }
}



A Few Hook Questions - El Forum - 02-06-2014

[eluser]CroNiX[/eluser]
What hook is this for?


A Few Hook Questions - El Forum - 02-06-2014

[eluser]xtremer360[/eluser]
To run during the whole CMS project. That way if the user isn't logged in then they can't access any of the pages.


A Few Hook Questions - El Forum - 02-06-2014

[eluser]CroNiX[/eluser]
No, which hook is this being called from...pre_system, pre_controller, etc.


A Few Hook Questions - El Forum - 02-06-2014

[eluser]xtremer360[/eluser]
Code:
$hook['pre_controller'] = array(
    'class'   =>  'Sys_prescript',
    'function' => 'initial_run',
    'filename' => 'Sys_prescript.php',
    'filepath' => 'hooks'
);



A Few Hook Questions - El Forum - 02-06-2014

[eluser]CroNiX[/eluser]
Just curious if it was the pre_system hook, as the Router isn't even loaded at that point. But it should be for pre_controller...so I'm not sure why that wouldn't work.

For troubleshooting, you could:
Code:
var_dump($this->CI->router);
die();
$current_class = $this -> CI -> router -> fetch_class();
and see if the router object exists. Other than that, I don't use hooks enough to be able to help more, and the hooks documentation doesn't really show much.



A Few Hook Questions - El Forum - 02-06-2014

[eluser]xtremer360[/eluser]
Trying to get property of non object then NULL


A Few Hook Questions - El Forum - 02-06-2014

[eluser]CroNiX[/eluser]
It doesn't look like the global CI object is available to some hooks. If you look at /system/core/CodeIgniter.php, you will see exactly the order that CI loads things, and when it fires certain hooks. Not everything is fully loaded depending on what hook you are trying to access.

Again, the hooks documentation is very lacking, which is probably why hooks are rarely utilized.

Has your friend who suggested you start down this path actually tried to do this? You might ask him/her how they did it.


A Few Hook Questions - El Forum - 02-06-2014

[eluser]CroNiX[/eluser]
http://stackoverflow.com/questions/2196559/codeigniter-hooks-pre-controller-loading-helpers


A Few Hook Questions - El Forum - 02-06-2014

[eluser]xtremer360[/eluser]
Wow that worked! Thank you. I've been doing days of research and have never seen that post.