• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Possible to access Custom Library from a Model?

#1
[eluser]rkhanna[/eluser]
I have a custom library I created that accesses my RESTful API. Is there some issue with using this library from a Model object?

I keep getting the following error:
Code:
Unable to load the requested class: apiservice

Strangely enough, the error has only occured when I deploy it to our test linux box (CentOS). It works perfectly fine on my Windows XP development environment.

The custom library class is called ApiService. The filename is ApiService.php in the /system/application/libraries/ folder.

I'm using the standard load in the model constructor
Code:
$this->load->library('apiservice');

Thanks in advance for any help.

#2
[eluser]cahva[/eluser]
I think this is because of case-sensitivity which windows filesystem is not. So, rename that ApiService.php to apiservice.php and your code should work.

#3
[eluser]rkhanna[/eluser]
Ok, but doesn't the documentation say to explicitly make sure the class name and file name are exactly the same? I assumed that meant case as well.

I'll give it a try though.

#4
[eluser]rkhanna[/eluser]
Ok, found the issue.

It doesn't like the two capitalization's in the file name. Apiservice.php works fine (as does apiservice.php).

I traced it to the Loader class; in particular the _ci_load_class function. As you can see below it only assumes the first letter of can be capitalized.

Code:
function _ci_load_class($class, $params = NULL)
    {    
        // Get the class name
        $class = str_replace(EXT, '', $class);

        // We'll test for both lowercase and capitalized versions of the file name
        foreach (array(ucfirst($class), strtolower($class)) as $class)
        {
        <snip>
    }

Thanks for the lead.

#5
[eluser]Colin Williams[/eluser]
..which is how the user guide stipulates it.

#6
[eluser]cahva[/eluser]
BTW, documentation also states(in the style guide) that classes should have only the first letter capitalized. So in that matter, ApiService should be Apiservice or Api_service Smile

But for the future, I recommend you always name your files lower-case. You wont have any problem that way when you copy from win server to linux server so thats atleast one thing you dont have to worry about when debugging.

#7
[eluser]rkhanna[/eluser]
I did just refactor it to be Api_service after Colin's post. That's when I checked the style guide and realized my mistaken ways Smile

So now the file is named Api_service.php, the class is named Api_service and everything is working on both Windows and Linux.

Thanks again all.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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