Welcome Guest, Not a member yet? Register   Sign In
Extend a controller Codeigniter
#1

I have installed Codeigniter on IIS. Everything is going great.
I am trying to extend the CI_Controller class.  The reason I am doing this is to put a security check in the constructor.  Some controllers will extend the CI_Controller.  Some controllers, which need to be secure will extend MY_Controller.  This seems like a good approach to me.

So far I have followed these instructions.
Creating Core Classes

But when I call a page which extends MY_Controller I get a blank page.
Check the network tab in Firebug, I see "500 internal server error."
I check my CI logs I see this:

Quote:DEBUG - 2014-11-24 10:26:47 --> Config Class Initialized
DEBUG - 2014-11-24 10:26:47 --> Hooks Class Initialized
DEBUG - 2014-11-24 10:26:47 --> Utf8 Class Initialized
DEBUG - 2014-11-24 10:26:47 --> UTF-8 Support Enabled
DEBUG - 2014-11-24 10:26:47 --> URI Class Initialized
DEBUG - 2014-11-24 10:26:47 --> Router Class Initialized
DEBUG - 2014-11-24 10:26:47 --> Output Class Initialized
DEBUG - 2014-11-24 10:26:47 --> Security Class Initialized
DEBUG - 2014-11-24 10:26:47 --> Input Class Initialized
DEBUG - 2014-11-24 10:26:47 --> Global POST and COOKIE data sanitized
DEBUG - 2014-11-24 10:26:47 --> Language Class Initialized

For a normal point of reference here is another page which does not extend MY_Controller
Quote:DEBUG - 2014-11-24 10:27:32 --> Config Class Initialized
DEBUG - 2014-11-24 10:27:32 --> Hooks Class Initialized
DEBUG - 2014-11-24 10:27:32 --> Utf8 Class Initialized
DEBUG - 2014-11-24 10:27:32 --> UTF-8 Support Enabled
DEBUG - 2014-11-24 10:27:32 --> URI Class Initialized
DEBUG - 2014-11-24 10:27:32 --> Router Class Initialized
DEBUG - 2014-11-24 10:27:32 --> No URI present. Default controller set.
DEBUG - 2014-11-24 10:27:32 --> Output Class Initialized
DEBUG - 2014-11-24 10:27:32 --> Security Class Initialized
DEBUG - 2014-11-24 10:27:32 --> Input Class Initialized
DEBUG - 2014-11-24 10:27:32 --> Global POST and COOKIE data sanitized
DEBUG - 2014-11-24 10:27:32 --> Language Class Initialized
DEBUG - 2014-11-24 10:27:32 --> Loader Class Initialized
DEBUG - 2014-11-24 10:27:32 --> Helper loaded: url_helper
DEBUG - 2014-11-24 10:27:32 --> Session Class Initialized
DEBUG - 2014-11-24 10:27:32 --> Helper loaded: string_helper
DEBUG - 2014-11-24 10:27:32 --> Session routines successfully run
DEBUG - 2014-11-24 10:27:32 --> Controller Class Initialized
DEBUG - 2014-11-24 10:27:32 --> Database Driver Class Initialized
DEBUG - 2014-11-24 10:27:32 --> Model Class Initialized
DEBUG - 2014-11-24 10:27:32 --> Model Class Initialized
DEBUG - 2014-11-24 10:27:32 --> File loaded: application/views/user/session_box.php
DEBUG - 2014-11-24 10:27:32 --> File loaded: application/views/public/public_menu.php
DEBUG - 2014-11-24 10:27:32 --> File loaded: application/views/public/welcome_message.php
DEBUG - 2014-11-24 10:27:32 --> File loaded: application/views/template.php
DEBUG - 2014-11-24 10:27:32 --> Final output sent to browser
DEBUG - 2014-11-24 10:27:32 --> Total execution time: 0.2092

Can anyone tell me what is wrong?
Reply
#2

(This post was last modified: 11-24-2014, 01:01 PM by ciadmin.)

You do have application/core/MY_Controller, right?

And you are calling /stuff, where application/controllers/Stuff.php contains class Stuff extends MY_Controller?

If this is the case, I suggest checking your IIS server logs for error messages.

ps - Newly registered users have their first couple of posts moderated, which means that they need to be approved before showing in the forum. You do NOT need to post twice. This is an effective anti-spam measure, though time-consuming at our end.
Reply
#3

I don't think you have your error reporting set up well, because an error causing a 500 HTTP status code will show some error, somewhere, either in output or in the logs.

Since there is no error provided, there are a couple of possible issues:
- application/core/MY_Controller.php does not exist (as ciadmin suggested)
- your controller is not extending MY_Controller (as ciadmin suggested)
- application/core/MY_Controller.php does not extend CI_Controller
- your subclass suffix is not set to "MY_" in application/config/config.php (has to be the same as your controller extension suffix, default is "MY_")
- syntax or other fatal error in your MY_Controller class
Reply
#4

You're overriding CI_Controller::__construct() without calling it.
Reply
#5

I have the same problem with you. And I found that my file name is not same as my class name. Try to check your filename. Filename must be MY_Controller.php
Reply
#6

Filename must be /application/core/MY_Controller.php (core dir doesn't exist by default, just create one)

It should look like:
PHP Code:
class MY_Controller extends CI_Controller {
  public function 
__construct()
  {
    
parent::__construct(); //need this!!

    //other construct code below here
  
}

  public function 
another()
  {
    
//some other function you want available to all other controllers that extend MY_Controller
  
}


Then other controllers:

/application/controllers/Something.php
PHP Code:
class Something extends MY_Controller {
  public function 
__construct()
  {
    
parent::__construct();

    
//other construct code below, if needed upon instantiation
  
}

Reply




Theme © iAndrew 2016 - Forum software by © MyBB