Doctrine 如何从DefaultController中的条令数据库获取所有数据,然后将这些数据转发到Symfony4中的细枝模板

Doctrine 如何从DefaultController中的条令数据库获取所有数据,然后将这些数据转发到Symfony4中的细枝模板,doctrine,Doctrine,我是Symfony4框架的新手,我不知道如何从发送给控制器的条令数据库中获取数据,然后将其呈现给twig模板 class DefaultController { private $twig; public function __construct(AuthorRepository $authorRepository, EntityFactory $entityFactory, Environment $twig, SerializerInterface $serializer

我是Symfony4框架的新手,我不知道如何从发送给控制器的条令数据库中获取数据,然后将其呈现给twig模板

class DefaultController
{

    private $twig;
    public function __construct(AuthorRepository $authorRepository, EntityFactory $entityFactory, Environment $twig, SerializerInterface $serializer )
    {
        $this->repository = $authorRepository;
        $this->factory = $entityFactory;
        $this->twig = $twig;
        $this->serializer = $serializer;
    }
    /**
     * @Route("/", name="homepage", methods={"GET"})
     *
     */
    public function homepageAction(): Response
    {
        $body1 = $this->twig->render('layouts/homepage.html.twig');
        print_r($body1);
        die;
    }
控制器必须返回Symfony\Component\HttpFoundation\Response对象,如果要使用twig模板,请使用Symfony\Bundle\FrameworkBundle\Controller\AbstractController扩展控制器类,并使用render方法


有关更多信息

为清晰起见,更新了文本
class DefaultController extends AbstractController 
{

/** @var AuthorRepository **/
private $repository;

public function __construct(AuthorRepository $authorRepository)
{
    $this->repository = $authorRepository;
}

/**
 * @Route("/", name="homepage", methods={"GET"})
 *
 */
public function homepageAction(): Response
{
    $authors = $this->repository->findAll();
    return $this->render('layouts/homepage.html.twig', ['authors'=>authors]);
}