1

I’ve read a lot of topic on this but i can’t figure why it is not working for me.

I’ve built a custom module. It is working fine since return $output; in my controller class is working : I can see in front-end a page with my custom html.

I’ve written a script outside Drupal in order to test. Basically, i connect to a webervice and retrieve all the datas. It’s working fine.

Now, i want to execute that script inside the controller class.

I need to load an external php library to instantiate an object.

In my test php file (outside Drupal) a simple require_once is working.

require_once('nusoap.php');

But it is not working in my controller class.

So, i’ve activated the library module and do :

$path = libraries_get_path('nusoap') . DRUPAL_ROOT . '/modules/contrib/webservices_tr/webservices_dc/src/Controller/nusoap.php';

require($path);

It’s still not working.

Error message indiquactes :

Error: Class 'Drupal\webservices_dc\nusoap_client' not found

I’m sur the file path is correct because if i do this, i can see the content of the file.

echo file_get_contents(DRUPAL_ROOT . '/modules/contrib/webservices_tr/webservices_dc/src/Controller/nusoap.php');
die();

I’ve also tried this :

in nusoap.php (where there is the class i need to include),

namespace Drupal\webservices_dc;

and in module controller class :

use Drupal\webservices_dc\nusoap_client;

But it is still not working.

Finally, i’ve copied the content of the external php library inside the module controller and it is working.

So i’m nearly sure the issue is related to the loading of that external library.

What is the correct way to load external php library (and wait it is fully loaded) in order to use it in a custom module ?

5
  • Are you using Drupal 7 or Drupal 8? You have tagged Drupal 8, but libraries_get_path() does not exist in Drupal 8. Maybe this is your issue.
    – Jaypan
    Commented Jul 20, 2017 at 8:28
  • @Jaypan I’m on Drupal 8. Library module is installed and libraries_get_path() doesn’t’ throw error (it is not undefined) and seems to load the file. Commented Jul 20, 2017 at 8:33
  • Why not use SoapClient instead of nusoap?
    – Kevin
    Commented Jul 20, 2017 at 11:24
  • @Kevin You’re absolutely right. It was my first intention to use SoapClient. I was able to connect but i wasn’t able to use methods. Since the client was using nusoap on his intranet, i decided to try it and i was able to use methods available. But it would be a better approach to use 'SoapClient'. Commented Jul 20, 2017 at 12:14
  • 1
    You can still pass calls through to __soapClient instead of call them dynamically, as that doesn't always work depending on the WSDL method involved.
    – Kevin
    Commented Jul 20, 2017 at 12:15

2 Answers 2

4

You cannot require classes with require or library hooks. Nor should you be editing external packages.

You are supposed to require external php packages with Composer. Composer will update the autoloader. Then in your class, you can say:

use ExternalPackage\namespace\Foo;

In the header and Foo class is then usable. Furthermore, you can inject it to the constructor with a little more work.

I would advise using SoapClient instead of nusoap as that is very old.

Regarding SoapClient, here is something I solved recently injecting it proper to my class, because that's the same road you're heading down.

Dependency inject a class with values from a config object?

3
  • Thanx ! I followed your advice, asked more informations from client and i was able to successfully connect and call method with SoapClient inside module controller. Commented Jul 20, 2017 at 21:53
  • I’ve made a lot of search before asking and it seems that there is a lot of answer on SO indicate to use library module with libraries_get_path or module_load_include('php', 'modulename','filename') Was it the way to include php file with Drupal 7 ? Commented Jul 20, 2017 at 21:55
  • 1
    It should also be pointed out that, for a ClassName class not using namespaces, the class should be referenced as \ClassName or PHP would look for the class in the currently used namespace, which is why PHP throws the error the OP (a.k.a. @SébastienGicquel) is seeing.
    – avpaderno
    Commented Jul 21, 2017 at 17:50
0

You can use module_load_include like we use it in Drupal7. Put your Php file in you module directory and use

  module_load_include('php', 'modulename','filename');
3
  • I’ve already tried this and it didn’t work (the class is not found). Maybe i’m doing something wrong. The library should be in the root of the module folder ? Commented Jul 20, 2017 at 9:48
  • No, this is wrong.
    – Kevin
    Commented Jul 20, 2017 at 11:32
  • Expanding on that - this is D8 and involves an external php package which will be in the vendor folder, and thus not discoverable by that function.
    – Kevin
    Commented Jul 20, 2017 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.