我正在开发一个自定义插件,它接受来自 api 的数据。
我正在尝试使用 twig 函数来获取如下数据
twig功能类:
<?php declare(strict_types=1);
namespace Nst\Twig;
use Nst\Service\StoryblokService;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class StoryblokTwigExtension extends AbstractExtension
{
public function __construct(private StoryblokService $storyblokService)
{
}
public function getFunctions(): array
{
return [
new TwigFunction('get_story', [$this, 'getStory']),
];
}
public function getStory(string $slug): array
{
return $this->storyblokService->getStory($slug);
}
}
也在我的服务中:
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services">
<services>
<service id="Nst\Service\StoryblokService" public="true">
<argument type="service" id="Storyblok\Client"/>
</service>
<service id="Storyblok\Client" class="Storyblok\Client">
<argument>%env(dumpcode)%</argument>
</service>
<service id="Nst\Storefront\Controller\CodesController" public="true">
<argument type="service" id="twig"/>
<call method="setContainer">
<argument type="service" id="service_container"/>
</call>
</service>
</services>
</container>
然后在我渲染页面的控制器中,我尝试注入到我的构造中以供使用。
<?php declare(strict_types=1);
namespace Nst\Storefront\Controller;
use Shopware\Storefront\Controller\StorefrontController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Environment;
/**
* @Route(defaults={"_routeScope"={"storefront"}})
*/
class CodesController extends StorefrontController
{
public function __construct(Environment $twig)
{
$this->twig = $twig;
}
public function setTwig(Environment $twig): void
{
$this->twig = $twig;
error_log('Twig has been set.'); // or use your preferred logging method
}
/**
* @Route("/mystore", name="frontend.example.example", methods={"GET"}, defaults={"_routeScope"={"storefront"}})
*/
public function showPage(): Response
{
if (!$this->twig) {
error_log('Twig is not set.');
}
return $this->renderStorefront('@Nst/storefront/page/example.html.twig', [
'example' => 'Hello world'
]);
}
}
我的 twig 页面的副本,我需要这个 twig 函数来显示从 api 消耗的数据。
{% sw_extends '@Storefront/storefront/base.html.twig' %}
{% block base_content %}
<h1>Our example controller!</h1>
{% endblock %}
{% set story = get_story('ola-landing-page') %}
{% if story %}
{# Display your story content here #}
{{ story.content.title }}
{# Add more fields as required from the story #}
{% endif %}
但我不断
hp.CRITICAL: Uncaught Error: Too few arguments to function Nst\Storefront\Controller\CodesController::__construct(), 0 passed in
Uncaught PHP Exception Shopware\Storefront\Controller\Exception\StorefrontException: "Class Nst\Storefront\Controller\CodesController does not have twig injected. Add to your service definition a method call to setTwig with the twig instance" at
我不知道该怎么办,已经坚持了很长时间,请帮忙
您可以通过添加needs_environment选项并将其设置为true来自动注入twig环境,然后正确的实例将自动传递给您的TwigFunction
<?php declare(strict_types=1);
namespace Nst\Twig;
use Nst\Service\StoryblokService;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class StoryblokTwigExtension extends AbstractExtension
{
public function __construct(private StoryblokService $storyblokService)
{
}
public function getFunctions(): array
{
return [
new TwigFunction('get_story', [$this, 'getStory'], [ 'needs_environment' => true),
];
}
public function getStory(\Twig\Environment $twig, string $slug): array
{
/*
Now you can use $twig to render something
or you could even pass it to the service if needed/wanted
....
return $twig->render(....);
$this->storyblockService->setTwig($twig);
...
*/
return $this->storyblokService->getStory($slug);
}
}