Getting started
Installation
composer require batteryincluded/batteryincluded-bundle
Enable the bundle, Symfony Flex does not have a recipe for it yet:
// config/bundles.php
return [
// ...
Batteryincluded\BatteryincludedBundle\BatteryincludedBundle::class => ['all' => true],
];
Credentials
Without configuration file the bundle reads the collection and the API key from the environment:
# .env.local, never commit credentials
BATTERYINCLUDED_COLLECTION=customer.my-shop
BATTERYINCLUDED_API_KEY=your-api-key
# optional, this is the default
BATTERYINCLUDED_BASE_URL=https://api.batteryincluded.io/api/v1/collections/
The API key grants write access to the collection. Keep it in .env.local, a secret or the environment of your
server, never in a committed file.
Check the connection:
bin/console bi:status
Collection
----------
------------- ----------------------------------------------------
Base URL https://api.batteryincluded.io/api/v1/collections/
Collection customer.my-shop
API key uIdm••••••••
Status 200 in 153 ms
Documents 730
Search time 7 ms
------------- ----------------------------------------------------
[OK] All collections are reachable.
Using the SDK services
All services of the SDK are registered and can be autowired: BrowseService, SuggestService, SyncService,
PresetsService, HighlightsService, RecommendationsService, CartRecommendationsService,
SimilarSearchService. The SDK documentation describes them.
use BatteryIncludedSdk\Suggest\SuggestService;
final class SuggestController
{
public function __construct(private SuggestService $suggestService)
{
}
}
First search page
A BrowseSearchStruct controller argument is filled from the query string, see Search in controllers:
use BatteryIncludedSdk\Shop\BrowseSearchStruct;
use BatteryIncludedSdk\Shop\BrowseService;
#[Route('/search')]
public function search(BrowseSearchStruct $searchStruct, BrowseService $browseService): Response
{
return $this->render('search.html.twig', ['result' => $browseService->browse($searchStruct)]);
}
<p>{{ result.found }} hits</p>
{% for hit in result.hits %}
<li>{{ hit.document._PRODUCT.name ?? hit.document._BLOG.title }}</li>
{% endfor %}
Next steps
- Deliver your products with a data provider and run
bin/console bi:export. - Keep the collection up to date with Doctrine or Messenger.
- Look at every API call in the web profiler.