使用 Symfony 显示用户列表

Show a list of users using Symfony

我正在尝试显示我的用户列表。我正在使用 FOSUser。这是我的控制器,我想在其中找到 fos_user table 中的用户列表并将其显示在我的 affiche 页面中:

<?php

namespace User\UserBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use User\UserBundle\Entity\User;
use Symfony\Component\HttpFoundation\RedirectResponse;
use User\UserBundle\Form\UserType;

use FOS\UserBundle\Controller\RegistrationController as BaseController;

class DefaultController extends BaseController
{
    public function indexAction()
    {
        $response = parent::registerAction();
        // ... do custom stuff
        return $response;
    }

    public function getDoctrine()
    {
        return $this->container->get('doctrine');
    }
         
    public function afficheAction()
    {   
        $em = $this->getDoctrine();
        $admins = $em->getRepository('UserUserBundle:User')->findAll(); 
        return $this->render('UserUserBundle:Default:affiche.html.twig', array('admins'=>$admins)); 
    }
}

但是我得到这个错误

Call to undefined method User\UserBundle\Controller\DefaultController::render() in C:\wamp\www\pfe2\src\User\UserBundle\Controller\DefaultController.php

我该如何解决?

您可能使用 FOSUserBundle 1.3.x,其控制器 classes 不从 FrameworkBundle 扩展基础 Controller class。因此,您不能使用 class 提供的快捷方式,但您必须像在基础 RegistrationController class:

中那样手动渲染模板
public function afficheAction()
{   
    $em = $this->getDoctrine();
    $admins = $em->getRepository('UserUserBundle:User')->findAll(); 

    return $this->container->get('templating')->renderResponse('UserUserBundle:Default:affiche.html.twig', array('admins'=>$admins)); 
}