Symfony 6:如何定义第三方包 twig ux 组件

问题描述 投票:0回答:0

我需要在我自己的供应商包中制作一个 symfony UX 组件。

有组件,在应用程序级别按需要工作:

<?php

namespace App\Components;

use Knp\Component\Pager\PaginatorInterface;
use Mysterty\CoreBundle\Repository\PostRepository;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

#[AsTwigComponent('latest_posts', template: '@MystertyCore/Core/_latest_posts.html.twig')]
class LatestPostComponent
{
    private PostRepository $postRepository;
    private PaginatorInterface $paginator;

    public function __construct(PostRepository $postRepository, PaginatorInterface $paginator)
    {
        $this->postRepository = $postRepository;
        $this->paginator = $paginator;
    }

    public function getPaginatedPosts()
    {
        $query = $this->postRepository->getOrdered();
        
        $pagination = $this->paginator->paginate($query, 1, 3);

        return $pagination;
    }
}

如果我按如下方式在

My/Bundle/src/Components
中移动它,它会崩溃,除非我设置服务并将我的组件定义为AbstractController:

#My/CoreBundle/config/services.yaml
services:
  My\CoreBundle\Components\LatestPostComponent:
    public: true
    autowire: true
    autoconfigure: true
    tags:
      - "controller.service_arguments"
      - "container.service_subscriber"
<?php

namespace Mysterty\CoreBundle\Components;

// […]

#[AsTwigComponent('mty_latest_posts', template: '@MystertyCore/Core/_latest_posts.html.twig')]
class LatestPostComponent extends AbstractController
{
    // […]
}

但是,它看起来很乱。这真的是正确的做法吗?

symfony components twig bundle symfony6
© www.soinside.com 2019 - 2024. All rights reserved.