Registering a new service
Edit on GitHubA service is a Spryker application layer shared by the Client, Zed, and Yves application layers. This service layer provides the ability to register a service once and have it applied to both layers. Usage is focused on level details (infrastructure layer)—for example, encoding, text processing, and sanitization. Currently, there are already a few Util
bundles providing services (UtilText and UtilEncoding).
How to use a service
To support best practices, any services shared between bundles and applications (Yves, Zed, Client) that do not resolve high-level business processes are moved to services. You can access services with the locator: $container->getLocator()->utilEncoding()->service()
.
Service structure
When creating a new service, under a newly-created module, follow this file structure:
src/Spryker/Service/
: Root directory.src/Spryker/Service/DemoService.php
: Main locatable service class. Should extendSpryker/Service/Kernel/AbstractService
.src/Spryker/Service/DemoServiceFactory.php
: Service factory class. Should extendSpryker/Service/Kernel/AbstractServiceFactory
.src/Spryker/Service/DemoDependencyProvider
: Service dependency provider. ExtendsSpryker/Service/Kernel/AbstractBundleDependencyProvider
.
Sample service class:
<?php
/**
* @method \Spryker\Service\UtilEncoding\UtilEncodingServiceFactory getFactory()
*/
class UtilEncodingService extends AbstractService implements UtilEncodingServiceInterface
{
/**
* {@inheritdoc}
*
* @api
*
* @param string $jsonValue
* @param int|null $options
* @param int|null $depth
*
* @return string
*/
public function encodeJson($jsonValue, $options = null, $depth = null)
{
return $this->getFactory()
->createJsonEncoder()
->encode($jsonValue, $options, $depth);
}
}
?>
- After creating all mentioned files, make the service visible by the locator autocompletion:
vendor/bin/console dev:ide:generate-service-auto-completion
Thank you!
For submitting the form