CakePHP and Flash

February 22, 2007

I have been meaning to add this to the resource list but I wanted to do it justice and give a good post. I shopped around for a while for a good PHP framework since I like PHP for middleware but I like my PHP object oriented and clean (embedded php is not really my thing). I looked at zend framework but I didn’t like that the it said .6 preview (I test a lot of beta stuff but a framework is something you have to commit to and I didn’t want that to be a beta).

CakePHP however is a gem. Based on ruby on rails it has some taken some useful features like scaffolding (a series of forms that take care of CRUD leaving you to free to work on business logic). It does a number of things automagically and I must admit that was one of the first sources of skepticism for me (I like/need to understand what is going on for me to use something effectively). The very nature of cakephp requires some discipline but at the same time it is not restrictive. It has a thriving community with discussion groups, live chat on irc (channel: irc.freenode.bet, room:cakephp) and an online resource called the Bakery were there are a number of resources for integrating other useful php classes into cakephp (like swiftmailer) or about cakephp itself. A lot of information about Cakephp can be had from the home website cakephp.org

While cakephp is targeted at developers writing php webapps with html frontend it is possible to use with flash as well. I have been doing just that with a project I have been working over the last month. The website is completely flash but integrates with cakephp through cakeamfphp (component used to integrate amfphp with cake). We needed amfphp to do some communication and I figured why not give cakeamfphp a whirl. Coincidentally Patrick Mineault had announced amfphp 1.9 beta and there was an example in the cakeamfphp repository with an example of how to integrate the two. The project is coming along well and I only had some minor problems migrating to a shared hosting environment (though it could be due to the insane setup…. even the server clock was wrong)

Pros of using Cake with flash:

  1. The scaffolding feature is useful for any developer really. The project manager was able to input test data while I worked on other stuff and that was extremely helpful
  2. Quick setup. The database connection is configured via a database.php and you can get started with just that (if you are doing local development). Other settings are changed in index.php (e.g. if you want to do a custom setup) and a core.php file.
  3. Rapid development. Once you get into the swing of things and you understand how to get a project started building out server-side logic can be quick (quick is subjective, I know). This becomes especially helpful in flash because flash projects often call for a great deal of design detail as well as programming attention. The less time spent on doing server-side work the more love your flash gets (after all a lot of users couldn’t care less how much/little effor went into the backend
  4. While it has conventions it is flexible and you are able to use other well known php classes with it (even some zend framework stuff).

Cons:

  1. To realize some of the benefits there are some conventions that you must follow. While this is not necessarily a bad thing itec takes some getting used to .. and in some cases it becomes important to know how to circumvent these conventions because not following them throws an error.
  2. There is some learning/getting used to stuff and I would estimate and the first project could be a testing the waters, the second you break even (in terms of time spent understanding things) and all other projects after you reap the full benefit.

Random pointers:

  1. Ensure that your server has mod_rewrite as well .htacces override privileges. Htaccess while not necessary for it to function it really is more than a nice to have.
  2. While there is a default setup that helps get up and running really quickly, I think doing a custom setup is worthwhile, especially if you have a structure for your flash projects already in place. There are instructions on how to accomplish this at the cakephp website.
  3. Make use of components that are available out there that may interface with popular classes (example there is a swift mailer component for sending mail). Why re-invent the wheel right?
  4. If you are using cakephp then you can make life even easier and use cakeamfphp for communication between the two. This way you get to make rpc calls and pass obejcts back and forth. (I like objects hehehehe).
  5. If sending post data to a cakephp controller in actionscript you should do:
    [as]var formdata:LoadVars = new LoadVars();
    formdata["data[field1]“];
    formdata["data[field2]“];
    [/as]
    so that in the it can be accessed via the controllers data property:
    [php]
    class SomeController extends AppController
    {
    function doSomething()
    {
    echo $data->field1;
    }
    }
    [/php]
  6. If a “Warning output buffers already set” warning is experienced it means that there is a space somewhere after a “?>” (this happened in the swiftmailer component)

4 Responses

  1. At work (The Iona Group) we are using CakePHP and Flash together ALOT! We are using both Flashvars and PEAR to get things into Flash instead AMFPHP… It seems to work well, especially for serializing etc.

    Good to know there are some other people out there doing the same sort of things… as niche as they might be.

  2. Hey,

    Just wanted to point out the website for the IRC chat… should be “.net”

    Great article otherwise :)

  3. The less time spent on doing server-side work the more love your flash gets (after all a lot of users couldn’t care less how much/little effor went into the backend.

  4. [...] By akeemphilbertAdd commentsProgramming, Uncategorized This post is in part a follow up to my CakePHP and Flash post but will contain what I hope is useful information for anyone shopping around for a PHP [...]

Leave a Reply