Skip to content

Injecting Services

Overview

This short guide covers how to use a service in the Shopware 6 Administration, including:

  • What an Administration service is
  • How to use a service

Prerequisites

All you need for this guide is a running Shopware 6 instance and full access to both the files and a running plugin. Of course you'll have to understand JavaScript, but that's a prerequisite for Shopware as a whole and will not be taught as part of this documentation.

Definition of an Administration service

Shopware 6 uses BottleJS to inject services. Services are small self-contained utility classes, like the repositoryFactory, which provides a way to talk to the API.

Injection of a service

A service is typically injected into a vue component and can simply be referenced in the inject property. This service is then available via its name on the object instance.

javascript
Shopware.Component.register('swag-basic-example', {
    // inject the service
    inject: ['repositoryFactory'],

    created() {
        // insatiate the injected repositoryFactory 
        this.productRepository = this.repositoryFactory.create('product')
    }
});
Was this page helpful?
UnsatisfiedSatisfied
Be the first to vote!
0.0 / 5  (0 votes)