views:

34

answers:

2

Hello,

This is my AdminBase.php

<?php

class AdminBase extends Controller{

    public function __construct(){
        parent::Controller();
        $admin = $this->session->userdata('username');
        if(!isset ($admin)){
            redirect('/Site/Home');
        }
    }

}

And this is my Admin Controller :-

<?php

class Admin extends AdminBase{

    public function index(){
        echo "You are in Admin panel!!";
    }

}

When i browse to Admin controller, i get this error :-

Fatal error: Class 'AdminBase' not found in C:\Program Files\wamp\www\College\application\controllers\Admin.php on line 3

+3  A: 

You need to put this line on top of Admin.php

<?php
// Include Base Controller
    include ('AdminBase.php');
class Admin extends AdminBase{

    public function index(){
        echo "You are in Admin panel!!";
    }

}

Also your base admin class should be like

<?php

class AdminBase extends Controller{

    public function __construct(){
        parent::Controller();
        $admin = $this->session->userdata('username');
        if(empty($admin)){
            redirect('/Site/Home');
        }
    }

}

Thanks

Muhit
Thanks Muhit, can you tell me why do i see `you are in admin panel` even though there is no session variable set? do you find any error with my AdminBase class?
Ankit Rathod
Yes if(!isset ($admin)) is always false. because $this->session->userdata('username'); will always return a value or Boolean so it should not be "!isset()" but might be "empty()" or check null or check false etc.
Muhit
Please look, I have put the changes within code.
Muhit
using `include` inside a controller is a bad practice in CodeIgnitor, You need to create a library and then load it.
Mithun P
+1  A: 

There is another way to do that just created filename MY_Controller.php at application/libralies

and then create like this

<?php
class AdminBase extends Controller {
    public function __construct(){
        parent::Controller();
        $admin = $this->session->userdata('username');
        if(!isset ($admin)){
            redirect('/Site/Home');
        }
    }
}
DominixZ