Welcome Guest, Not a member yet? Register   Sign In
A Few Hook Questions
#1

[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
        {
        }
    }
}
#2

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

[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.
#4

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

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

[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.
#7

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

[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.
#9

[eluser]CroNiX[/eluser]
http://stackoverflow.com/questions/21965...ng-helpers
#10

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




Theme © iAndrew 2016 - Forum software by © MyBB