0

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

1
  • you can use the hmvc in codeingniter which is good then extends the class Commented Jan 17, 2014 at 11:48

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

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?
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.
using include inside a controller is a bad practice in CodeIgnitor, You need to create a library and then load it.
1

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');
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.