• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MY_Controller and autoload

#1
[eluser]JonoB[/eluser]
This one has me completely baffled. I set up a test CI project on my localhost. As part of this, I created a file in application/core/my_controller.php as follows:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

Code:
class MY_Controller extends CI_Controller
{
    function __construct()
    {
        parent::__construct();

        $this->output->enable_profiler(TRUE);
        
        $this->session->set_userdata(array('company_id' => 1));
        
        if ( ! $this->tank_auth->is_logged_in())
        {
            redirect('/auth');
        }
    }
}

?>

And my controllers then extend this as follows:

Code:
class Products extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
    }
  
    function index()
    {      
        //do something here      
    }
}

And this works absolutely 100% fine.

I then moved my files onto my live webserver, and kept getting a WSOD when trying to load any controller/function. No error message, no 404, nothing. I eventually tracked this down to My_Controller class - it was not autoloading on my live server. So, to double check I put the following line right at the top of each of my controllers:

Code:
require_once '/home/something/public_html/application/core/my_controller.php';
And, voila, all controllers now work fine.

So, why the hell does this work fine in my localhost (xampp running PHP 5.3.1) and not on my live server (PHP Version 5.3.3)? Am I missing something really obvious here?

#2
[eluser]InsiteFX[/eluser]
MY_Controller has to be in application/core

Add this to the bottom of application/config/config.php
Code:
/*
| -------------------------------------------------------------------
|  Native Autoload - by Phil Sturgeon.
| -------------------------------------------------------------------
|
| Nothing to do with config/autoload.php, this allows PHP autoload to work
| for base controllers and some third-party libraries.
|
| Place this code at the bottom of your application/config/config.php file.
*/
function __autoload($class)
{
    if (strpos($class, 'CI_') !== 0)
    {
        @include_once(APPPATH . 'core/' . $class . EXT);
    }
}

InsiteFX

#3
[eluser]JonoB[/eluser]
Thanks for the reply.

I had tried all of the above with no luck. Many times.

And then...after much keyboard bashing I realised that windows is not case sensitive, and linux is. Hence why it was working on localhost and not on live server.

So my_controller.php should have been named MY_Controller.php. Doh!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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