Welcome Guest, Not a member yet? Register   Sign In
library classes
#1

[eluser]sipsniffa[/eluser]
Hi,

Is there any way to automatically load user libraries that are located under application/libraries/?

Also, I've been unable to load user libraries from within a controllers constructor. Is this expected?

Thanks.
#2

[eluser]Randy Casburn[/eluser]
hi sipsniffa,

Your answer is in the excellent documentation found here: http://ellislab.com/codeigniter/user-gui...aries.html

If you've already read all that and still can't get it to work, please rephrase your question and provide some code examples to give us something to go by and we'll be happy to help.

Welcome to CI!

Randy
#3

[eluser]sipsniffa[/eluser]
Thanks, Randy.

I see no mention of auto-loading user-libraries in the docs so I assume it isn't possible.

I attempted loading a user-library from a controller's constructor without any success (it loads fine from the controller's other functions):
Code:
class My_Controller extends Controller {

    function My_Controller() {
        parent::Controller();
        session_start();
        $this->load->library('My_Controller_Services');
    }

    // ...
}
That resulted in:
Code:
An Error Was Encountered

Unable to load the requested class: My_Controller_Services
I don't see any mention of loading being restricted to non-constructor functions, so I wondered if there was any real reason why it might be that way (i.e. is this a bug or a tiny gap in the docs?). Anyway, nothing critical.

Thanks.

Paul.
#4

[eluser]jaswinder_rana[/eluser]
In the link Randy mentioned, it does tell you how to create your own libraries and how to include them.

If your library is in application/libraries, then you can use $this-.load->library('your_library_file') which will load application/libraries/your_library_file.php and create object of class Your_library_file (Note capital Y) in that file.


From your example, you should have a "My_controller_services" class in application/libraries/My_Controller_Services.php file.
#5

[eluser]sipsniffa[/eluser]
Thanks, jaswinder.

Yes, I acknowledged that I can create and load user-libraries from all controller functions except the controller's constructor. It was just a small point, but thought it may be worth making.

Paul.
#6

[eluser]jaswinder_rana[/eluser]
I am not sure what you mean, but you should be able to load any library from your constructor. It should work (I am doing it).

Also, if there is a library that you want to load in EVERY controller then you might want to put it in application/config/autoload.php. Look for library array and put your library name there. It's well commented. http://ellislab.com/codeigniter/user-gui...oader.html
#7

[eluser]sipsniffa[/eluser]
Jaswinder,

Now that is interesting! Although I have loaded a user-library from a controller function, I haven't managed to auto-load it and nor have I been able to load it from a controller's constructor, so I assume I'm doing something half-right!

I place my library PHP file under application/libraries/, name it with capitalisation and name the class with exactly the same name as the PHP file name - including capitalisation. Attempting to load the library from a controller's constructor fails (see my earlier message, above).

Can you offer any code snippets showing how you load a user-library from a controller's constructor, please? Any other tips welcome, too!

Thanks.
#8

[eluser]Randy Casburn[/eluser]
Hi sipsniffa,

You've simply not followed the naming convention called for in the documentation. That's what jaswinder_rana pointed out to you.

Please simplify everything for now just to humor us. Change your class and file name to one word like "Test".

You'll find that $this->load->library("test"); will work perfectly when you have a library in your application/libraries folder named "Test.php".

Try that.

Randy
#9

[eluser]jaswinder_rana[/eluser]
application/library/sample_lib.php (_lib is not important, Just used it here to have a unique class name)

- If you are on a UNIX system (or a case-sensitive system unlike Windows) then I suggest you use lower cases for file OR JUST FIRST character capital like Sample_lib.php

- Class's first character MUST be capital (not sure about the rest; same as filename I guess)

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

class Sample_lib{
  //Your code
}

application/controllers/Sample.php

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

class Sample extends Controller{
function Sample(){
  parent::Controller(); //MUST Call it WHEN you use constructor in YOUR controller

  $this->load->library('Sample_lib');
  $this->Sample_lib->function();

  //OR
  $this->load->library('Sample_lib','something');
  $this->something->function();

}
}

This is how it works. I may have some syntax or logical error but other than this is the way I did it.

Hope this helps


EDIT: Shucks. Randy beat me to it. I agree, it may be your naming convention.
#10

[eluser]Randy Casburn[/eluser]
[quote author="jaswinder_rana" date="1229389823"]application/library/sample_lib.php (_lib is not important, Just used it here to have a unique class name)[/quote]

This part is extremely important! This is what is causing the naming convention problem.

If you create libraries with names like My_Bestest_Library_On_The_Planet_Earth, it is really hard to determine how to find a file that is named the same. So the CI developers put together some very simple rules that you have to follow. You just have to follow them. Folks get very inventive with the names and then can't figure out why things won't work.

So whenever this happens go back to simplicity and you'll find that it isn't CI that is causing the problems, it is usually us that are not follow the simple rules.

Here is how CI determines the file name from the load->library call:

Code:
// We'll test for both lowercase and capitalized versions of the file name
foreach (array(ucfirst($class), strtolower($class)) as $class)

As you can see, there is no way the something like Special_Class will work.

Please be cautious with underscores and only capitalize the FIRST letter of your classes and libarary file names.

Randy




Theme © iAndrew 2016 - Forum software by © MyBB