This is probably a bit niche but anyone especially au fait with the element api here?

On project X I have a custom module within which I am referencing a service from another plugin (bolden/google-maps-api). If I access the module directly (eg via a controller or variable belonging to my module) then all works fine.

However - if I try and access my module service from within an element api endpoint, then the service doesn't work (or rather returns null when trying to access the third-party plugin/service).

For example, in my endpoint I am calling the service ($location is a string):

$data = MyModule::getInstance()->ServiceName->ServiceMethod( $location );

But this returns the following Response: Call to a member function placeFromText() on null (the stack trace highlights the corresponding line within my module/service), eg:

$googleMaps = GoogleMapsApi::getInstance();
$result = $googleMaps->googleMapsApiService->placeFromText( $place, 'geometry' );        

The same query, when accessed within my module (eg a controller or variable), works fine and returns a data object from the Google Maps API (as expected).

Bit confused - anyone have any thoughts on why this service isn't accessible from my module when accessed via the element API?

Any thoughts on this issue would be much appreciated!


  • I don't have a fix in your particular case but I had the same problem once, this might put you on the way - craftcms.stackexchange.com/a/13533/3630 - This line "You can think of Services and Variables as two different APIs for a plugin – one is only made available to PHP (Services) and the other is only made available to Twig/templates (Variables)."
    – Oli
    Commented Feb 10, 2019 at 11:23

1 Answer 1


You have to use full namespaces in Element API because anything within those functions is out of scope. So you need to use the full path to the Google class.

$googleMaps = new \Vendor\Module\GoogleMapsApi();

Putting a "use" directive at the top of the file will not work.

I had the exact same problem and that resolved my issues immediately.

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.