Adding Custom Panels to the CakePHP Status Plugin

Intro
A few weeks ago I released a status plugin for CakePHP. It is basically a single page where you can get a quick snapshot of your app. The plugin ships with a few random panels, but it’s easy enough to add your own. This post will tell you how.
Make sure to get the latest version from GitHub. Also note that the way to include the default panels has changed. You now have to include the “Status.” prefix.
requestAction vs Ajax
There are two different ways to add a panel – using requestAction or Ajax. The general rule is if the panel is pretty fast to load and content is static use requestAction. If the panel takes some time or you want to be able to interact with it then use Ajax. For example, the logs panel uses requestAction since it’s pretty quick to grab the log entries and you really don’t need to interact with them. The Google Analytics panel, on the other hand, takes a few seconds to query the API and has options to change the timeframe, so it uses Ajax.
Packaging Your Panel
Panels can be packaged as plugins or just files in your app. For this example we’ll just put the files as part of the app. Making your panel as a plugin is pretty much the same, you just need to put all the files in the plugin’s directory and include the plugin name as a prefix when setting which panels to use. Check out the click plugin for an example. I’m just going to cover the requestAction approach today. Look for another post of using Ajax soon.
The requestAction Way
The Element
Let’s build a panel that shows the last 10 users that signed up for your app. The first thing you need is an element. In /app/views/elements/panels (note the extra directory level – this is optional, but helps to keep things clean) create a file called latest_users.ctp’. In that file put:

<?php
$users = $this->requestAction(array('controller' => 'users', 'action' => 'latest'));
?>

The Controller
In the users controller we now need to add an action called latest that will get the user data and pass it back to the element.

function latest() {
return $this->User->find('all', array('order' => 'created desc', 'limit' => 10));
}

Since there is no view associated with this action anyone trying to access it directly with just get an error. If you wanted to extra sure that no one unauthorized could exploit this action you could add this to the beginning:

if (!Configure::read('Status.allow')) {
die;
}

Back To The Element
Now you have a list of $users so it’s just a matter of displaying them. Here’s the complete element:

<?php
$users = $this->requestAction(array('controller' => 'users', 'action' => 'latest'));
?>

<?php __('Latest Users') ?>
<?php if($users) { ?>

<?php foreach($users as $user) { ?>

<?php echo $user['User']['email'] ?>
<?php echo $time->timeAgoInWords($user['User']['created']) ?>

<?php } ?>

<?php } else { ?>
Your app has no users...loser!
<?php } ?>

Including Your Panel
Now you just have to include your panel on the status dashboard. In /app/bootstrap.php add the line:

Configure::write('Status.panels', array('panels/latest_users'));

Here’s What It Looks Like