Welcome Guest, Not a member yet? Register   Sign In
Problems extending Controller...
#1

[eluser]Bleeding Edge Productions[/eluser]
Hi guys,

I am trying to extend Controller with a class MY_Controller. My code is:

Code:
class MY_Controller extends Controller {
    
    /**
     * Instantiate any required site-wide variables
     */
    protected $base;    // Base URL of site
    protected $assets;    // Path to assets folder
    
    // --------------------------------------------------------------------
    
    /**
     * Constructor - sets site-wide variables
     */
    public function __construct() {
        
        parent::Controller(); // Call the parent constructor
        
        $this->base = $this->config->item('base_url');
        $this->assets = $this->config->item('assets');
        
    }
    
}

This is saved as application/libraries/MY_Controller.php

Then, I have a controller called Homepage:

Code:
class Homepage extends MY_Controller {
    
    /**
     * Constructor - inherits site-wide variables from MY_Controller.
     */
    public function __construct() {
        
        parent::__construct();
        
    }
    
    // --------------------------------------------------------------------
    
    /**
     * Generate the page
     */
    function index(){
        
        /**
         * Assign variables to the $pagevars array
         */
        $pagevars['base']        = $this->base;
        $pagevars['assets']        = $this->assets;
        
        /**
         * Load the view
         */
        $this->load->view('main', $pagevars);
        
    }
    
}

saved as application/controllers/Homepage.php

The view (main.php) is a simple HTML file echoing the variables $base and $assets.

But, it seems not to be working, and I think it's something to do with the way I'm extending Controller, because having "class Homepage extends Controller" works (aside from the PHP errors because $base and $assets are not defined), but using MY_Controller doesn't work.

Any help appreciated!

Thanks,

Martin
#2

[eluser]n0xie[/eluser]
What errors are you getting? Did you try to change the filename to 'homepage.php' ?
#3

[eluser]Bleeding Edge Productions[/eluser]
Sorry - do you mean the main.php view file? It works fine as is when Homepage extends Controller, just not when it extends MY_Controller.

As far as errors go - I get a blank page served to the browser. Nothing at all in it. Nada Smile

Cheers,

Martin
#4

[eluser]slowgary[/eluser]
I could be wrong, but it's probably throwing an error and you're just getting a blank screen because of error reporting settings. In class Homepage, you're attempting to access protected members of the parent class ($base and $assets). If you MUST have them protected, you should defined methods in the base class to get them. Set them as public and see if that fixes the problem, then figure out how you'd like to proceed.

FYI, only the file needs to be named MY_Controller, the class inside can be named anything you want. In my application, I actually have several classes in my MY_Controller file: BaseController, PublicController and PrivateController (public and private each extend base). Then my actual controllers always extend either Public (frontend) or Private (admin/backend).
#5

[eluser]Bleeding Edge Productions[/eluser]
I tried changing the members to public and still had no joy.

However, based on this Phil Sturgeon's article here: http://philsturgeon.co.uk/news/2010/02/C...ing-it-DRY I managed to get it to work by adding this to the bottom of the config file:

Code:
/*
| -------------------------------------------------------------------
|  Native Auto-load
| -------------------------------------------------------------------
|
| Nothing to do with config/autoload.php, this allows PHP autoload to work
| for base controllers and some third-party libraries.
|
*/
function __autoload($class)
{
if(strpos($class, 'CI_') !== 0)
{
  @include_once( APPPATH . 'libraries/'. $class . EXT );
}
}

What concerns me is that I have no idea why it's necessary, and would like to understand! I don't like just copying and pasting code! :roll:

Thanks!

Martin
#6

[eluser]slowgary[/eluser]
You shouldn't need to do that. __autoload() is one of PHP's magic functions: http://us.php.net/__autoload

The basic idea is that if you call a class that hasn't been defined yet, PHP makes a call to __autoload() (if you've defined it), which gives you one last opportunity to load the required file before PHP croaks with an error (class not defined).

BUT, look at your config.php and make sure you have this:
Code:
$config['subclass_prefix'] = 'MY_';

Also, check out the documentation:
http://ellislab.com/codeigniter/user-gui...asses.html
http://ellislab.com/codeigniter/user-gui...aries.html


CodeIgniter will automatically load the "MY_Controller.php" file if it exists in "application/libraries". If your "__autoload()" function is fixing the problem, that means that CodeIgniter is not loading the file itself. This would lead me to believe that the file is not in the right place, or it's not named correctly based on the config prefix, maybe the CaSE is wrong or something along those lines.
#7

[eluser]Bleeding Edge Productions[/eluser]
Bizarre... Works on CI 1.7.2. I was using 2.0. Could be something to do with the application folder being outside the system folder by default in 2.0. Not sure though...

M
#8

[eluser]WanWizard[/eluser]
You have noticed that in 2.0, core extensions (and that includes MY_Controller) go into the core directory, and not in the libraries directory?
#9

[eluser]Bleeding Edge Productions[/eluser]
This would be one of those 'D'oh!' moments... Big Grin

Thank you!




Theme © iAndrew 2016 - Forum software by © MyBB