layout | title |
---|---|
default |
How a Client Uses a Service |
Before interacting with a service, you must be familiar with the service interface, including input/output data structures defined by the corresponding schema. Services are defined by PHP interfaces, so you can simply acquire an instance of the service and invoke a method directly. You can also access services using REST and SOAP calls.
To directly invoke a service using its PHP interface, use dependency injection. To do this, simply define a constructor dependency on the service you need. From there, you can use the instance to directly invoke methods on the interface.
Because a service call typically accepts one or more primitive types or service data objects, you might have to convert or extract them before calling the service. For an example, see public function getInputData
in this Web API class.
For example, if you know a customer's ID and want to get data about the customer, here's what you do:
-
Search the customer account service interface for the getCustomer method
(public function getCustomer($customerId);)
-
Notice in the comment
@return \Magento\Customer\Service\V1\Data\Customer
. This is the service data object. -
Get the details about the response from the service data object.
Service methods complete in one of two ways:
-
Return a response, which can be:
- Null if no data is returned
- Array
- Primitive type (such as an ID)
- Service data object. A service data object holds only primitives or other service data objects and so on.
-
Throw an exception
An exception should be handled according to logic associated with the method invocation and the error received. Most of the time, the client logs only the details of the root exception and reports the error to the user in an appropriate way.
Dependency injection is a technique used in object-oriented programming. When working with Magento software, dependency injection means you define a constructor dependency on the service you need. From there, you can use the instance you receive to invoke whatever methods you need directly. Because this concept might be new to PHP developers, we recommend the following references:
- Magento dependency injection
- Instantiating and Injecting Helpers in Magento 2 by Alan Storm
- Magento 2: Injecting Interfaces by Alan Storm
- Video—The Clean Code Talks: Don't Look For Things!
- Video—The Clean Code Talks: Global State and Singletons
Any service that has an app/code/[Vendor]/<ModuleName>/etc/webapi.xml
can be accessed using REST and SOAP. (Additional tasks, not discussed here, are required to access services using SOAP.)