Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all users using FosUserBundle?

Tags:

symfony

I am using symfony2 and FosUserBundle as a user manager. I want to display all users. I have tried the code below but it returns a blank page.

How can I fix it?

controller :

<?php

namespace Annuaire\AdminBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class AdminController extends Controller {

    public function indexAction() {
        return $this->render('AnnuaireAdminBundle:Admin:index.html.twig');
    }

    //get all users
    public function usersAction() {
        //access user manager services 

        $userManager = $container->get('fos_user.user_manager');
        $users = $userManager->findUsers();

        return $this->render('AnnuaireAdminBundle:Admin:users.html.twig', array('users' =>   $users));
    }

}

twig :

<h1>get all users</h1>
{% for user in users %}
<li>{{ user.username|e }}</li>
{% endfor %}
like image 472
Taieb Baccouche Avatar asked Aug 03 '13 18:08

Taieb Baccouche


1 Answers

Silly me. This:

$userManager = $container->get('fos_user.user_manager');

Should be:

$userManager = $this->get('fos_user.user_manager');

You will still want to get your error reporting turned on for your application. This should have tossed an "unknown variable" error message and would have been trivial to track down.

like image 73
Cerad Avatar answered Oct 20 '22 04:10

Cerad