Good morning everybody.
I am trying to get a module inside my app up and running, but when I inject „Client“ in the controller function signature, I get the error: Cannot autowire argument $client.
An action button is working fine. There its possible to inject „Client“ and „Event“ without any problems.
module declaration inside of my manifest.xml
<module name="productExport"
source="http://192.168.79.135/productExport">
<label>Product Export</label>
<label lang="de-DE">Produkt Export</label>
</module>
and the controller (nothing special):
/**
* @Route("/productExport", name="productExport", methods={"GET"})
*/
public function productExport(Client $client): Response
{
return $this->render('Product/product.export.html.twig', [
'test' => bin2hex(random_bytes(16))
]);
}
If I remove „Client“, the contents of the template is shown. I am totally desperate and have no clue how to solve this issue.
I Appreciate any kind of help.
Danny
Guessing based on your obvious use domain (Export) you probably want to inject a service you use for authentication against the API, which one exactly is impossible to tell (Shopware\Core\Framework\Store\Services\StoreClient maybe? Check your PSR-4 records).
In order to properly access the desired service, you have to do a proper DI with the services.xml syntax (<argument type="service" id="shopware.store_client"/>)
and the actual injection into the controller (private var and assigning the injected service to an attribute of $this).
„If I remove „Client“, the contents of the template is shown. I am totally desperate and have no clue how to solve this issue.“ Well… That’s because Symfony does its job concerning the render() method.
Hi Jonas,
thanks for your response. I forgot to mention, that I am trying to implement an App and not an Extension.
Danny
An app is an extension, just not a plugin.
So which „Client“ do you inject now? That’s still the first issue to solve.
I am using the AppTemplate:
use App\SwagAppsystem\Client;
May you show some more code? Like if use App\SwagAppsystem\Client;
is included etc.
Sure. Here is the complete Controller:
<?php declare(strict_types=1);
namespace App\Controller;
use App\Repository\ShopRepository;
use App\SwagAppsystem\Client;
use App\SwagAppsystem\Event;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class WebHookController extends AbstractController
{
/** @var ShopRepository $shopRepository */
private $shopRepository;
public function __construct(ShopRepository $shopRepository)
{
$this->shopRepository = $shopRepository;
}
/**
* @Route("/productExport", name="productExport", methods={"GET"})
*/
public function productExport(Client $client): Response
{
return $this->render('Product/product.export.html.twig', [
'test' => bin2hex(random_bytes(16))
]);
}
}
Manifest.xml (and other potentially existing files) content please.
Here is the complete xml:
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/shopware/platform/master/src/Core/Framework/App/Manifest/Schema/manifest-1.0.xsd">
<meta>
<name>testApp</name>
<label>Test App</label>
<description>Test</description>
<author>Test</author>
<version>1.0.0</version>
<license>proprietary</license>
</meta>
<setup>
<registrationUrl>http://192.168.79.135/registration</registrationUrl>
<secret>c7cca700f03344a7354f9d3c320adb1c</secret>
</setup>
<webhooks>
<webhook name="AppDeleted" url="http://192.168.79.135/applifecycle/deleted" event="app.deleted"/>
<webhook name="AppInstalled" url="http://192.168.79.135/applifecycle/installed" event="app.installed"/>
</webhooks>
<admin>
<module name="productExport"
source="http://192.168.79.135/productExport">
<label>Product Export</label>
<label lang="de-DE">Produkt Export</label>
</module>
<action-button action="sendOrder" entity="order" view="list" url="http://192.168.79.135/sendOrder">
<label>Send selected Orders</label>
</action-button>
</admin>
</manifest>
- Controllers used in Apps have App\Controller as namespace and don’t inherit from AbstractController.
- You can’t DI inject the shopRepository here in that fashion, because you don’t invoke it in service.xml/DI-container.
You put concepts together which don’t get along with each other, check this out for the start: GitHub - shopwareLabs/AppExample
So, actually there is no need to inject the „Client“. In a module you just can get the shopdata from the request query.