Welcome Guest, Not a member yet? Register   Sign In
Extending 'Controller' Class issue
#1

[eluser]ericsodt[/eluser]
I have created an admin section and I am trying to authenticate the user at the Controller level. I created a class called 'MY_AdminController' and use it as my extended controller within my action class
Code:
class Admin extends MY_AdminController {

    function Admin()
    {
        parent::MY_AdminController();    
    }
}

My new class looks like
Code:
class MY_AdminController extends Controller {

    function MY_AdminController() {    
        parent::Controller();
        // authentication below    
    }    
    
}

I have not autoloaded it and I am not manually loading it. From what I am seen this is done for you through the core system files. Is this correct? The reason I am asking is because I am getting the following error msg.

Code:
Fatal error: Class 'MY_AdminController' not found in <my_site>\system\application\controllers\admin\admin.php on line 3

any help would be greatly appreciated.

Thanks!
#2

[eluser]wabu[/eluser]
Should it be "MY_Controller" instead?
#3

[eluser]ericsodt[/eluser]
I would like it to stay 'My_AdminController' since it is the controller of the admin section. Does the name matter though?


[quote author="wabu" date="1250191987"]Should it be "MY_Controller" instead?[/quote]
#4

[eluser]jedd[/eluser]
[quote author="ericsodt" date="1250191777"]I have created an admin section and I am trying to authenticate the user at the Controller level. I created a class called 'MY_AdminController' and use it as my extended controller within my action class[/quote]

It's hard to provide too much information when it comes to trouble-shooting .. but very easy to provide too little.

For example ... where have you put this file?

If it's not in your libraries directory, then I think we've identified your problem.

Note also that camel case is eschewed by the CI style guide, and a corollary of that is that upper-case letters (other than the configurable prefix (MY) and the first letter (A)) are also considered bad form according to the manual.
#5

[eluser]Johan André[/eluser]
Yes it matters!

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

class Admin_controller extends MY_Controller
{
   function __construct()
   {
      parent::__contstruct();
   }
}

Place in the same file (libraries/MY_Controller.php) and you'll get the functionality you want...

EDIT: As for the naming I would recommend not to camelcase. Check the userguide (especially the styleguide-section).
#6

[eluser]ericsodt[/eluser]
If it matters how then do I create a controller named AdminController and extend Controller from it?

I'll be needing more than just one controller class that extends 'Controller'




[quote author="Johan André" date="1250192945"]Yes it matters!

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

class Admin_controller extends MY_Controller
{
   function __construct()
   {
      parent::__contstruct();
   }
}

Place in the same file (libraries/MY_Controller.php) and you'll get the functionality you want...

EDIT: As for the naming I would recommend not to camelcase. Check the userguide (especially the styleguide-section).[/quote]
#7

[eluser]Colin Williams[/eluser]
The class name doesn't matter, but the filename and placement does, if you want it to automatically load. You can of course always do the loading yourself with include() or require(). That's how a lot of frameworks work, but since CI does some handy convention-based auto-loading, many people seem hesitant to load stuff themselves (or they don't know how...)

You can just play into the convention and name your file MY_Controller.php, and CI will load it automatically, and so then your MY_AdminController class will be available, but you might want to keep the one class per file and name it whatever you want. In that case, you need to require it yourself.

Code:
require APPPATH .'libraries/MY_AdminController'. EXT;

class Admin extends MY_AdminController {

    function Admin()
    {
        parent::MY_AdminController();    
    }
}
#8

[eluser]ericsodt[/eluser]
EXACTLY what I was looking for, thank you!!




[quote author="Colin Williams" date="1250203321"]The class name doesn't matter, but the filename and placement does, if you want it to automatically load. You can of course always do the loading yourself with include() or require(). That's how a lot of frameworks work, but since CI does some handy convention-based auto-loading, many people seem hesitant to load stuff themselves (or they don't know how...)

You can just play into the convention and name your file MY_Controller.php, and CI will load it automatically, and so then your MY_AdminController class will be available, but you might want to keep the one class per file and name it whatever you want. In that case, you need to require it yourself.

Code:
require APPPATH .'libraries/MY_AdminController'. EXT;

class Admin extends MY_AdminController {

    function Admin()
    {
        parent::MY_AdminController();    
    }
}
[/quote]
#9

[eluser]louis w[/eluser]
I would refrain against using MY_ in controller names unless you are customizing a built-in CI library. Personally, when I extend the base controller I give it a totally different name. Otherwise it's going to get confusing.

I have an Application controller which extends Controller. This is the general bootstrapping of my application, never called directly. That way I just extend Application when I want a new Controller. I find it a much more elegant and obvious solution.
#10

[eluser]Colin Williams[/eluser]
I agree with Louis and so I typically go with the require/include method.




Theme © iAndrew 2016 - Forum software by © MyBB