Welcome Guest, Not a member yet? Register   Sign In
Extended Controller class not found - stumped!
#11

[eluser]philpem[/eluser]
[quote author="dmorin" date="1256147788"]Having different file names and class names is a sure way to confuse yourself or anyone else who may have to maintain it down the road. Is that really better than just including the file at the top, or better yet, adding an autoloaded? The answer to this will be different for everyone, but I wouldn't mix names; the potential for confusion is just too high.[/quote]

For some reason, autoloads don't seem to work for user/application libraries (specifically, MY_ class libraries). Maybe I'm doing something wrong, but just adding "MY_FoobarController" to $autoload['libraries'] (in /application/config) doesn't seem to work...

I have moved the 'application' directory out of 'system', i.e. my directory tree has gone from this:
- system
- application
- config
- controllers
- ...
- user_guide

to:
- application
- config
- controllers
- ...
- system
- cache
- codeigniter
- database
- ...
- user_guide

Is it possible that this might be causing issues with class loading? "MY_Controller" seems to load fine from /application/libraries...
#12

[eluser]dmorin[/eluser]
Sorry, I wasn't clear. MY_Controller loads fine because CI looks for any extending classes using the "MY_" (or other configured prefix) before loading the base class.

When I said autoloading, I didn't mean CI's autoloading, I meant PHP's autoloading. See http://php.net/manual/en/function.spl-au...gister.php

It's only available in PHP5 and while there are some downsides, it's also hugely convenient in certain cases.
#13

[eluser]Phil Sturgeon[/eluser]
I think philpem & dmorin are argueing at cross purposes here.

dmorin: You are right, I always use __construct when I can instead of Classname but there is a problem. __construct is a method, and if that method is not used when the class is written then you cannot call it. The only way you can use both is if you do the following when writing classes:

Code:
<?php
class Something {

   function __construct()
   {
      // do your main construct code
   }

   function Something()
   {
      $this->__construct();
   }
  
}
?>

Sadly CodeIgniter doesnt have this so you cannot call __construct with any CodeIgniter code you have not written yourself.

Make sense? :-)

Also, instead of writing several classed in one file, I do something like this:

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

// Code here is run before ALL controllers
class MY_Controller extends Controller
{    
    function MY_Controller()
    {
        parent::Controller();
        
                // shared logic for ALL controllers
        }
}

include(APPPATH . 'libraries/Public_Controller'.EXT);
include(APPPATH . 'libraries/Admin_Controller'.EXT);

I hope some of that can help you.
#14

[eluser]dmorin[/eluser]
@phil Sturgeon

Quote:Sadly CodeIgniter doesnt have this so you cannot call __construct with any CodeIgniter code you have not written yourself.

That's what I thought also, but it's not correct. parent::__construct doesn't only call a method named __construct, it calls the constructor regardless of which naming convention it uses. Try it, I didn't realize that was the case either until I tried it.
#15

[eluser]Unknown[/eluser]
The information here is stale. MY_Controller class must be saved in Applications Core folder for it to work in CodeIgniter 2.O not in Libraries folder




Theme © iAndrew 2016 - Forum software by © MyBB