How to build a “dashboard” for your application in CakePHP

As an addition to my recent post, I wanted to share a technique, which should allow you to pretty easily build a “dashboard” for your app.
The dashboard would basically grab some information from a few different models and display it all on one page.

First, let’s go over some assumptions…

  1. We are not going to use requestAction(), because it’s the “worst” (and absolutely the last-resort approach). It has it’s purposes, but we want to avoid it as much as possible. (I’m not going into detail as to why, because that has been covered many times in many many places, so you can just fire-up your trusty search engine to learn more).
  2. The models from which we’ll gather the information are absolutely not related to one another, if they are related… this is not the best approach. Remember, we don’t want to reload any models (objects), which CakePHP has already loaded for us.

So, keeping both of those things in mind, here’s what we’ve got:

Post model, with a method getTop();
News model, with a method getRecent();
Employee model, with a method getTopPerformers();
Product model, with a method getTopSellers();

(You are welcome to come up with your own methods or use your imagination as to what they might return).
Even though it’s hard to believe that we have no relationship between any of the above models, we’ll assume that to be true for the sake of this example…

We are going to build a Dashboard Controller, with a single action (for now), called index() it’s only purpose is to grab the info from all of the above models and “set” it for the view.

Here’s what it might look like:

<?php
    class DashboardController extends AppController {
          
          var $name = 'Dashboard';
          var $uses = array();
          
          function index () {
               $this->set('topPosts', ClassRegistry::init('Post')->getTop());
               $this->set('recentNews', ClassRegistry::init('News')->getRecent());
               $this->set('topEmployees', ClassRegistry::init('Employee')->getTopPerformers());
               $this->set('topSellingProducts', ClassRegistry::init('Product')->getTopSellers());
          }
    }
?>

And that’s really it. Now we have all of our Models’ info available for the view, and we can display it any way we’d like.

Just a few things to point out:

  • We are using ClassRegistry::init('ModelName') to load the relevant models, only once we need them. The init() method will automagically instantiate the model object.
  • We are avoiding the use of the $uses array(), although it could have been used just as well, but this way the models are loaded only when required, and if you have additional actions that might need some other set of models, there is no need to “overburden” your app by loading something extra, before it’s actually needed.
  • If you’d like to make this new dashboard into your actual homepage of the site, please see my other post on how to setup the appropriate route.

A little note about the new line character, Ajax and a silly mistake…

Just something “fun” to share on Friday…

I had a simple Ajax auto-complete type of widget in my app… which ultimately returned this view:


if(!empty($items)) {
     foreach($items as $key => $item) {
       echo $item . '\n';
     }
 }

Everything worked well, except that \n was not treated as a new line character. So, all items were returned as one long string, rather than being broken down into “individual” lines in the auto-complete widget.

After some wall and head connections, I’ve found out that instead of '\n' I have to use double quotes "\n" to get this to work as expected. Problem solved.

Hopefully it’ll help someone out there :)

CakePHP and Aptana Cloud = Love at First Sight

Before I write a post, I usually test the code I write about… and try to copy/paste it the best I can… so I don’t look like an ass later on.

This post, however, I simply cannot prepare for…

I’m just way too excited (yes, even more than Pacquiao’s dismantling of De La Hoya last week) about the topic, which deals with CakePHP in a littler manner, but mostly focuses on my new found love: “Aptana Cloud”.

Aptana IDE has been my choice in web development for a little while now (Eclipse users take note, all other IDE users… at least give it a shot ;)). It has solid support for JS, CSS and PHP. Thankfully, I don’t care about Java anymore, thus it works out perfectly well. Just the ability to insert a color hex value from a tiny built-in pallet, while working on a CSS file… puts a smile on my face every time.

I do love such attention to detail, but just to make things a little better, it also has support (i.e. auto-complete, proper highlighting and other time-saving features for coders) for jQuery (which I use) as well as top popular JS frameworks such as ExtJS, Prototype, … etc., so it appears like the Aptana team is “on top of web dev things” ;)

Alright, that seems like a few good-enough reasons to make Aptana my top IDE.

Well… all of a sudden (to me, which means I’m way behind the trends) Aptana folks introduced a cloud computing service, that kicks major ass as far as I can see… while testing it for about two days.

So this post will be just about my ventures into Aptana Cloud, setting up a cake app and being all happy about the potential with such a service.

(… guess I should have prefaced this post by saying that I’m not a paid actor :))

Alright… so first, if you’d like, you can read about Aptana Cloud (and watch a nice little video) here: http://www.aptana.com/cloud.

Now, I’m pretty much going to outline my baby-steps with Aptana Cloud + CakePHP and what I did (and learned a little) to launch a CakePHP application on Aptana Cloud.

1.Why bother and what is this all about?

To me, when working with multiple clients or multiple developers (or even on a single project), management of deployment/synchronization and consistency between development, staging and production somehow always caused a little pain. (I hope most people follow dev->stage->production release cycle ;)) Of course, if you have IT guys, or your are an admin systems guru, managing servers becomes a lot easier, but even still… if one is focused on web development, is it worth the time and effort to somehow worry about the state of your server (Email, Database, SVN, this, that, etc.)? (To me, at least, it always seemed like a boring and mundane task, sorry to all the geeky sysops ;) ).

There had to be a better way…

2.Creating a cloud… and whatta f… is this “cloud”?

Somehow I found out about the Aptana Cloud in my IDE…

After reading up on the service… I thought something along the lines of: “Well, that might address my pain just mentioned above… why not give it a shot?”.

Without boring you with anymore details in the IDE it’s basically 3 clicks and your cloud is setup…

… Which means, you’ve got a fully working:

Staging Server, Production Server and Private Server.

They are all up and running with SVN, SSH access, Postfix (for outgoing emails only, as far as I know). Oh yeah, all up and ready in *real-time*… and $0 for you to spend. (GTFO, you might think)… well, you get a 21 day free-trial. (Don’t even have to pull out a CC… that’s people thinking about doing things right… another smile).

3.So how to get my app running?

Well, I SSH’d into the “virtual” (cloud) server (right from the IDE too;)) and probably did a bunch of things wrong, clicked around here and there and after deliberation decided to do the following…

Obviously I need the cake core up there, and the default doc root is:


/home/aptana/homes/teknoid/staging/web/htdocs

I thought the “right” way to install an app in this scenario was to put the cake core in the “web” directory and then *sync* my app into htdocs.

After that, I’d set the doc root (which is easily changed right in IDE) to:


/home/aptana/homes/teknoid/staging/web/htdocs/webroot

4.Getting the cake core installed

After kicking around some ideas for a while, I decided to use the latest SVN repo from CakePHP. Thankfully, the SVN is already good to go in the cloud so in my:


/home/aptana/homes/teknoid/staging/web/

I did:


svn checkout https://svn.cakephp.org/repo/branches/1.2.x.x/cake/

Alright, I get the cake core in the place where I hope it should be.

5.Syncing up the app from Aptana

Now I need to get my project (app) uploaded to the staging server. That’s what I think I did next…

Right click the project name in my IDE’s project browser, went to “Synchronize” (you can decide if you wish to sync up your local or development project to public or staging server).

The IDE takes a “minute to think” and then it creates a sync-up dialog. Since it was my first time uploading the project, all files had to be created and after some “syncing up” my app was ready to run on the test server. And it did! :)

6.Following up

I realized that syncing-up app/config and app/tmp directories (as some other folders/files) probably isn’t a good idea, and thankfully you can easily add such exceptions in the IDE.

….

Well, that covers my little venture into CakePHP and Aptana Cloud. As you can guess so far it has been a very positive experience.

7.Some random notes

  1. First and foremost, to me, is the “team” option… Say, I have a project that I cannot finish due to laziness or whatever other strange reasons. I can easily “delegate” the coding to someone else with a simple SVN account setup and possibly some free beer. That type of “delegation and collaboration” pain is simply removed from the equation.
  2. I found that Apatna team is very responsive and open to suggestions, so if you feel like sharing some ideas on how it could benefit the community and CakePHP (but more importantly, your exciting life as a web developer), throw them on the Aptana forums or even here.
  3. I’m still very early in my testing, there are probably lots of things I miss or don’t know yet.
  4. The service is improving daily, so I think following the solid growth patterns of CakePHP, these kinds of projects I do really enjoy because it’s an amazing learning experience.
  5. I’ve tested the “sync” option numerously and it works quite well, I’ve not found a single problem… other than that it’s a bit slow (at least for me) and I could see about 3 UI improvements :)
  6. Give it a shot!
  7. Git support should be there any day now ;)
  8. I think the “Private” server is meant for your own stats and overall cloud management (i.e. don’t sync your CakePHP projects there).
  9. Yep, you get tools like phpMyAdmin to manage your DB (or internal IDE DB browser), all the obvious that you need to use the terminal… and well so far that’s all I’ve needed :)
  10. And I hear that there are some very interesting things coming down the line in Aptana… concerning some PHP frameworks, the IDE and the app deployment ;)