Adding Your Own Panels to the Status Plugin

Intro

A few weeks ago I released a status plugin for CakePHP. It is basically a single page where you can go and get a quick snapshot of different parts of your web app. The plugin ships with a few random plugins, but it’s easy enough to add your own. This post will tell you how.

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 static use requestAction. If the page 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.

The requestAction Way

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
  $data = $this->requestAction(array('controller' => 'users', 'action' => 'latest'));
?>