• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to load the requested class

#1
[eluser]briang[/eluser]
In my application/libraries/ directory...

I am trying to extend my "MY_Controller" class with a "MY_Auth" class.

The MY_Controller class I have already created works great. But when I create any other classes I continually get this error:
Quote:An Error Was Encountered
Unable to load the requested class: Auth

The steps I am taking:

1) I create the file named MY_Auth.php, save it to 'application/libraries/'
2) Its code:
Code:
class MY_Auth extends Controller{
function __construct(){
  parent::__construct();
}
}

3) Open the config/autoload.php and change this line to contain my new class:
Code:
$autoload['libraries'] = array('database','auth');

Then get the error. I don't understand how to get past this.

Also, why did I not have to add MY_Controller to the $autoload[] array? And it works just fine. I have tried not adding MY_Auth, but then it errors out as well.

Please help.
Thank you.
Brian

#2
[eluser]InsiteFX[/eluser]
You have the wrong name MY_Auth should be MY_Controller but the class name you can name it anything you like.

Then you extend it in your new Controllers. When you save it the name would be MY_Controller in the application/libraries folder.

So if you wanted an Auth contoller you would do this:

class Auth extends Controller

Then save it as MY_Controller in the application/libraries folder.

Then when you create a new controller

class New extends Auth

Enjoy
InsiteFX

#3
[eluser]briang[/eluser]
Thanks for the reply InsiteFX.

So you can only create a MY_Controller.php for application/libraries/ ?

No other library files can be created along with it?

#4
[eluser]InsiteFX[/eluser]
No you can create other libraries but you can only have one MY_Controller

But you can have different directories see the CodeIgniter user guide here:

Managing your Applications

Enjoy
InsiteFX

#5
[eluser]briang[/eluser]
I read through the information contained in the Managing your Applications link. Though didn't see anything relating to this issue.

Must the other files contained in applications/libraries/ also have the "MY_" prefix? (Or whatever it is defined as in config).

It seems I cannot load any classes except what is defined in "MY_Controller.php".

Attempting to load any other class with a different file name continually brings up this error:
Quote:An Error Was Encountered
Unable to load the requested class: whateverclassname

Thanks again for your help, InsiteFX.
Brian

#6
[eluser]briang[/eluser]
OK, I think I understand the "MY_" prefix better now. It is only for extending native CI classes.

Other than that, library files do not need the prefix. Correct?

I was trying to (excuse my terminology) "double extend" the Controller.

Theoretically, I was trying to do this: "Auth extends MY_Controller extends Controller".
Was attempting this by creating an Auth.php that extends MY_Controller, while having the MY_Controller.php extend Controller. Then in application/controllers/, create a controller class 'New extends Auth', to gain a level of authentication.

#7
[eluser]n0xie[/eluser]
[quote author="briang" date="1254588014"]OK, I think I understand the "MY_" prefix better now. It is only for extending native CI classes.

Other than that, library files do not need the prefix. Correct?

I was trying to (excuse my terminology) "double extend" the Controller.

Theoretically, I was trying to do this: "Auth extends MY_Controller extends Controller".
Was attempting this by creating an Auth.php that extends MY_Controller, while having the MY_Controller.php extend Controller. Then in application/controllers/, create a controller class 'New extends Auth', to gain a level of authentication.[/quote]
This can be achieved, but you have to let CI know where your Auth base controller is.

The 2 most methods is adding it to MY_Controller or adding an include in MY_Controller:
Code:
// method 1
// file: MY_Controller.php
class MY_Controller extends Controller {}
class Auth extends MY_Controller {}

// method 2
// file: MY_Controller.php
class MY_Controller extends Controller {}
include APPATH . '/libraries/auth.php';

// file: auth.php
class Auth extends MY_Controller {}

Once you have this in place you can build controllers like you would expect:

Code:
// admin.php in your controller folder
class Admin extends Auth {}

Hope this clears things up.

#8
[eluser]briang[/eluser]
Perfect! Thanks n0xie.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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