I am following a course about shopware 6 on how to display data throughout the storefront, which would be provided with an API controller. I already checked the code multiple times with the tutorial code and I can not find any mistakes. The API client Insomnia is able to recognize the API route, nevertheless, the server responds with an internal server error.
"detail": "Call to a member function search() on null",
"meta": {
"trace": [
{
"file": "\/Applications\/MAMP\/htdocs\/shopware6_testing\/custom\/plugins\/SwagShopFinder\/src\/Core\/Api\/DemoDataController.php",
"line": 63,
"function": "getActiveCountry",
In fact, I am quite new to Symfony and I am not able to read and understand the code fluently. I assume there is something wrong with the method getActiveCountry, but I am not sure how to engage this.
The response that I should be getting is 204 HTTP_NO_CONTENT, throughout the function generate().
public function generate(Context $context): Response
{
$faker = Factory::create();
$country = $this->getActiveCountry($context);
$data = [];
for ($i = 0; $i < 50; $i++) {
$data[] = [
'id' => Uuid::randomHex(),
'active' => true,
'name' => $faker->name,
'street' => $faker->streetAddress,
'postCode' => $faker->postcode,
'city' => $faker->city,
'countryId' => $country->getId(),
];
}
$this->shopFinderRepository->create($data, $context);
return new Response('', Response::HTTP_NO_CONTENT);
}
Domain Route (proposed by the shopware 6 Docs.):
/** * @RouteScope(scopes={"api"}) */
API Route:
* @Route("/api/v{version}/_action/swag-shop-finder/generate", name="api.custom.swag_shop_finder.generate", methods={"POST"})
The endpoint /generate should trigger the function generate(), which continually generates 50 shops stored in an array and give the server Respond 204 HTTP_NO_CONTENT.
The API controller is registered in a services.xml and a separate routes.xml file:
<service id="SwagShopFinder\Core\Api\DemoDataController" public="true">
<argument id="country.repository" type="service" />
<argument id="swag_shop_finder.repository" type="service" />
<call method="setContainer">
<argument type="service" id="service_container" />
</call>
</service>
routes.xml file:
<import resource="../../**/Api/*Controller.php" type="annotation" />
If more information is needed I am happy to share it!