Session Library not loaded in extended controlloer

#1
[eluser]Cally[/eluser]
Hello all,

First time posting here and loving the CodeIgniter framework.
I have been extending the standard controller with a bunch of methods that make page to page execution easier and more convenient but I've run in to a snag with one of them.

For some reason, the extended controller can't find the models/libraries on certain pages and throws up this error:

Severity: Notice
Message: Undefined property: B2_Controller::$User
Filename: libraries/Loader.php
Line Number: 1035

What puzzles me is that they are loaded in the contructor before anything is executed as well as autoloaded before execution even starts (debugging attempt) so why are they undefined? I tried to load the CI base as a reference in to the constructor with get_instance but didn't have much luck their either.

I don't usually come asking for help as I can often find the answers some where on this forum. Smile

Anyway, I'll post the contructor and the methods that seem to be related.

Code:
class B2_Controller extends Controller {

    public function B2_Controller() {
        parent::Controller();
        $this->load->model("User_model", "User");
        $this->load->library("session");
        $this->defaultLinks();
        $this->defaultMenus();
        $this->defaultJavascriptVars();
        $this->rememberLog();
    }

    protected function setSession($Username, $Password, $Remember = false) {
        $cUser = $this->User->checkUser($Username, $Password);
        if($cUser) {
            $this->session->set_userdata(array('user_id' => $cUser['user_id'], 'user_name' => $cUser['user_name'], 'user_password' => $cUser['user_password']));
            if($Remember) {
                set_cookie('username', $Username, 31536000, '.192.168.1.4', '/');
                set_cookie('password', $Password, 31536000, '.192.168.1.4', '/');
            }
            return true;
        } else return false;
    }

    protected function unsetSession() {
        $this->session->unset_userdata(array('user_id' => '', 'user_name' => '', 'user_password' => ''));
        delete_cookie("username");
        delete_cookie("password");
    }

    protected function isLoggedIn() {
        if($this->session->userdata('user_id') && $this->session->userdata('user_name') && $this->session->userdata('user_password')) {
            $cUser = $this->User->checkUser($this->session->userdata('user_name'), $this->session->userdata('user_password'));
            if($cUser) return true;
        }
        return false;
    }

    protected function rememberLog() {
        if(!$this->isLoggedIn()) {
            $Username = get_cookie("username");
            $Password = get_cookie("password");
            if($Username && $Password) {
                if($this->User->checkUser($Username, $Password))
                $this->setSession($Username, $Password, true);
                else $this->unsetSession();
            }
        }
    }



}

#2
[eluser]InsiteFX[/eluser]
You should autoload the session library in application/config/autolaod.php
This allows sessions through out your application.

Also you need the default index function!

All the code you have there should be in your own library...
Not in the controller.

InsiteFX

#3
[eluser]Cally[/eluser]
The libraries are all auto loaded, I added it in to the constructor to see if that would fix it. The reason for all the methods in the controller are to keep it all in one place as it's used on every page for displaying.

So this is what still confuses me, even though the Session library is loaded, it still throws up errors.

#4
[eluser]Cally[/eluser]
Ok, I've moved the session stuff in to an extended library of the original session class to keep everything neat, everything seems to be working fine now which I find a bit odd to be honest.

#5
[eluser]Cally[/eluser]
Ok, I have split all things relating to sessions in an extending sessions class and all visual aspects in to a class called Display.

Another issue remains, the Display class controls all menus and needs to make a call to the session class to check if the user is logged in or not as so to determine what menus should be displayed.

Now the same Non-object error is occuring even when trying to load the library manually from the constructor. I've never had so much trouble with something that should be quite simple.

#6
[eluser]InsiteFX[/eluser]
If it's used in all controllers then you want to create a MY_Controller
and extend the MY_Controller!

Extending Core Classes

InsiteFX

#7
[eluser]Cally[/eluser]
That's what I've been doing B2 is the prefix I'm using instead of MY. The problem has been averted for now but I expect I may run in to similar issues with the libraries not being able to access other libraries or visa versa. Unless that's the wrong approach, I'm not sure.

If I have any issues in the future, I'll either update this thread or create a new one. Tongue
Feel free to speculate on why I couldn't access the autoloaded libraries from within B2_Controller methods though, it seems a bit odd.

I'm guessing the libraries aren't loaded until after B2_Controller is loaded or something similar as it's the only thing that could explain the problem to me.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.