<p>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.</p>

<pre><code>$googleMaps = new \Vendor\Module\GoogleMapsApi();
</code></pre>

<p>Putting a "use" directive at the top of the file will not work.</p>

<p>I had the exact same problem and that resolved my issues immediately.</p>

CC BY-SA 4.0
2019-02-13T16:13:09.623Z
2019-02-13T16:13:09.623Z
Tony DeStefano
<p>This is probably a bit niche but anyone especially au fait with the element api here?</p>

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

<p>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).</p>

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

<p><code>$data = MyModule::getInstance()-&gt;ServiceName-&gt;ServiceMethod( $location );</code></p>

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

<pre><code>$googleMaps = GoogleMapsApi::getInstance();
$result = $googleMaps-&gt;googleMapsApiService-&gt;placeFromText( $place, 'geometry' );        
</code></pre>

<p>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).</p>

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

<p>Any thoughts on this issue would be much appreciated!</p>

<p>Cole</p>

CC BY-SA 4.0
2019-02-10T11:23:59.013Z
I don't have a fix in your particular case but I had the same problem once, this might put you on the way - https://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
<plugin-development><craft3><plugin-elementapi><modules>
Element API calling Module that integrates with third-party plugin
# Element API calling Module that integrates with third-party plugin

(Asked by: username_0 on 2019-02-07)

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!
Cole

**Tags:** plugin-development, craft3, plugin-elementapi, modules

> **Comments:**
> **username_1**: I don't have a fix in your particular case but I had the same problem once, this might put you on the way - https://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)."  

---

## 1 answers

### Answer by username_2 on 2019-02-13. Score: 1 | Accepted answer

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.


---

