phpdashboarduserfrosting

UserFrosting: Creating various dashboard widgets that have access to data


[UserFrosting: 0.3.1]

Hello,

I am currently exploring the UserFrosting environment and architecture.

I am trying to create 'widgets' on the main dashboard (dashboard.twig) which need access to various data.

e.g. A widget to display a simple list of logged on users.

The default main dashboard doesn't seem to have access to the data (besides current user), because Slim configuration (index.php) doesn't use any controller. Is this correct?

If I want to use functions/data from multiple controllers (e.g. users, groups etc..), how do I pass on this data to the main dashboard?

Thanks


Solution

  • If you are referring to this route for /dashboard, then you are correct - the default setup does not use an external controller class:

    $app->get('/dashboard/?', function () use ($app) {    
        // Access-controlled page
        if (!$app->user->checkAccess('uri_dashboard')){
            $app->notFound();
        }
    
        $app->render('dashboard.twig', []);          
    });
    

    You have a few choices. You can add a new method to an existing controller class, or even create a new controller class (note that after adding a new class, you will need to run composer update to add it to the autoloader).

    Alternatively, you could just add your controller logic directly into the route closure above. The Eloquent models User, Group, etc can be accessed just as easily in the route closure as in a controller class.

    Finally (and this is what I would recommend for generating a list of users), you could generate the dashboard page first using one request, and then fetch the actual data with a separate request, building the list using client-side code (Javascript). This is actually how the /users page works by default.

    You'll notice that if you visit the /users page with your browser console open, that it makes a separate request to /api/users. This returns a JSON object containing the list of users. For example:

    {
        "count": 5,
        "rows": [
            {
                "id": 1,
                "user_name": "admin",
                "display_name": "Overlord",
                "email": "mrdj@userfrosting.com",
                "title": "The New Kid",
                "locale": "en_US",
                "primary_group_id": 2,
                "flag_verified": 1,
                "flag_enabled": 1,
                "flag_password_reset": 0,
                "created_at": "2015-10-21 00:00:00",
                "updated_at": "2016-06-22 17:14:57",
                "last_sign_in_time": "2016-07-19 19:23:49",
                "sign_up_time": "2015-10-01 00:00:00"
            },
            {
                "id": 22,
                "user_name": "armin",
                "display_name": "Armin van Buuren",
                "email": "armin@userfrosting.com",
                "title": "Cannon Fodder",
                "locale": "nl_NL",
                "primary_group_id": 0,
                "flag_verified": 1,
                "flag_enabled": 0,
                "flag_password_reset": 1,
                "created_at": "-0001-11-30 00:00:00",
                "updated_at": "2015-12-07 12:37:14",
                "last_sign_in_time": 0,
                "sign_up_time": 0
            },
            {
                "id": 20,
                "user_name": "david",
                "display_name": "David Guetta",
                "email": "pierre@userfrosting.com",
                "title": "Pyromancer",
                "locale": "en_US",
                "primary_group_id": 4,
                "flag_verified": 1,
                "flag_enabled": 1,
                "flag_password_reset": 0,
                "created_at": "-0001-11-30 00:00:00",
                "updated_at": "-0001-11-30 00:00:00",
                "last_sign_in_time": 0,
                "sign_up_time": 0
            },
            {
                "id": 13,
                "user_name": "deadmau5",
                "display_name": "Deadmau5",
                "email": "deadmau5@userfrosting.com",
                "title": "DJ Extraordinaire",
                "locale": "en_US",
                "primary_group_id": 3,
                "flag_verified": 1,
                "flag_enabled": 1,
                "flag_password_reset": 0,
                "created_at": "-0001-11-30 00:00:00",
                "updated_at": "2016-03-29 14:37:24",
                "last_sign_in_time": 0,
                "sign_up_time": 0
            },
            {
                "id": 23,
                "user_name": "digweed",
                "display_name": "John Digweed",
                "email": "digweed@userfrosting.com",
                "title": "DJ of the Future",
                "locale": "en_US",
                "primary_group_id": 3,
                "flag_verified": 1,
                "flag_enabled": 1,
                "flag_password_reset": 0,
                "created_at": "-0001-11-30 00:00:00",
                "updated_at": "-0001-11-30 00:00:00",
                "last_sign_in_time": 0,
                "sign_up_time": 0
            }
    }
    

    We then use some custom code here and here to transform this data for use with tablesorter. Doing it this way has the advantage that you can perform pagination on the server side, rather than loading the entire list of users in one request and forcing the user to wait while the entire table is built (which could take a very long time when you have lots of users!)