CakePHP 3 - Setting data from table to default.ctp


Hi, I’m trying to set data from a table to default.ctp. So in AppController.php, I added:

public function beforeFilter(Event $event)
    $usersTable = TableRegistry::get('Users');
    $users = $usersTable->getAllUsers();

    $this->set('users', $users);

And in default.ctp:

<?php foreach($users as $user){ echo $user['first_name']; } ?>

But apparently $users was not set. I also tried adding this in PagesController.php but I got a blank screen after doing so:

public function beforeFilter(Event $event)

I need some help to figure out what I’m missing, any help would be greatly appreciated. Thanks!


Hello blinkdodger,

it sounds like you don’t have debugging enabled (you can do so in config/app.php file). The DebugKit is a very great tool.

However, I’m not sure if you can set view-variables in the beforeFilter callback. You might try beforeRender instead (works for me though).

Also, $this->log($users) could help you to identify the root cause of your issue.

Hope this helps