0

The class is here (with instructions).

I have it added to application/libraries/randomorg

When I add $this->load->library('RandomOrg\Random'); to my controller's constructor, I get the following error.

Message: Class 'RandomOrg\Client' not found

I have tried adding the various files before that (such as Client.php), but that doesn't help.

What is going on and how do I fix it?

desbest
  • 4,746
  • 11
  • 51
  • 84
  • Namespaced libraries can't be loaded that way in CI. You need to require and autoloader and use that to load it. – Alex Mar 24 '18 at 04:20
  • I don't know much about OOP. How do I load a namespaced library in Codeigniter? – desbest Mar 24 '18 at 04:23
  • I have loaded the library with this code. https://gist.github.com/JeyKeu/7533af3b9b5fd078910d Now how do I use the class? I cannot use `$rand_org = RandomOrg\Random($this->config->item('randomorg_apikey'));` as it does not recognise `RandomOrg/Random` – desbest Mar 24 '18 at 04:28

1 Answers1

1

I know I've answered this question before I just can't find where and don't know what to search. So here it is: Codeigniter can only load single php file libraries (excluding drivers which is a different thing entirely). To load this kindof library (namespaced) you have to use something like: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md (class example).

Let's call it Autoloader_psr4 and save it in libraries (modify the class declaration to match this name verbatim (e.g. Autoloader_psr4). Remove the namespace declaration in the class so it looks like: https://pastebin.com/NU8Rbp7Y

Let's also move all the files in src/randomorg/ to just be in a folder in third_party called RandomOrg e.g. application/third_party/RandomOrg. Your folder should look like the contents here: https://github.com/defiant/randomorg/tree/master/src/randomorg

Usage:

$this->load->library('autoloader_psr4');
$this->autoloader_psr4->register();
$this->autoloader_psr4->addNamespace('RandomOrg', APPPATH . 'third_party/RandomOrg');
$random = new \RandomOrg\Client(); // or whatever...
Alex
  • 9,215
  • 8
  • 39
  • 82
  • I'm having problems getting this to work. Inside `application/libraries/Autoloader_psr4.php` when I comment out `namespace Autoloader_psr4;` the web page loads, but when I leave it in I get this error `Non-existent class: Autoloader_psr4` – desbest Mar 24 '18 at 04:46
  • Yes I forgot to mention to remove the namespace declaration. Here is the version I use: https://pastebin.com/NU8Rbp7Y ... Let me know if you are still having issues with it – Alex Mar 24 '18 at 04:48
  • Now when I run `$bw = $random_org->generateIntegers($this->config->item('randomorg_apikey'), 2, 0, 50);` in my php controller I get this error `Message: require(C:\Users\User\Documents\UniServerZ\www\fresherplay\application\libraries/RandomOrg/RuntimeException.php): failed to open stream: No such file or directory` – desbest Mar 24 '18 at 04:54
  • Where are your RandomOrg src files currently? – Alex Mar 24 '18 at 04:58
  • application/third_party – desbest Mar 24 '18 at 05:01
  • mkay i'm just not sure why I see this then in your error: `application\libraries/RandomOrg/RuntimeException.php` can you post your folder tree on imgur? also double check you are using `APPPATH . 'third_party/RandomOrg'` – Alex Mar 24 '18 at 05:02
  • I changed it to `third_party` and I still get the same error.`Message: Class 'RandomOrg\RuntimeException' not found` `C:\Users\User\Documents\UniServerZ\www\fresherplay\application\third_party\RandomOrg\Client.php` – desbest Mar 24 '18 at 05:04
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/167450/discussion-between-alex-and-desbest). – Alex Mar 24 '18 at 05:06
  • I fixed it by enabling the mod_ssl and mod_curl extention for php. Also ensure that all API calls point to `https://` instead of `http://` – desbest Sep 05 '18 at 10:49