The Idea Basket
“When you find yourself wanting to jump on a design trend, ask this simple question: does this really work for me? Sometimes a trend is just a style that appeals to small number of people.”
— Jared White

September 2006

Willowgarden Highlight: Easy Processing of Form Input

September 19th, 2006

Welcome to this week's installment of Willowgarden Highlights. I'll be talking about processing form input and saving it via an archetype (domain model object), a very common task of any Web application.

In the bad old days of processing forms and saving each field to a column in a database row, you'd have to do a lot of manual gruntwork: accessing each field from the $_POST superglobal, checking the values, constructing the SQL query, connecting to the database and executing the query, and so forth. Wouldn't it be great if you could just import the form right into an object, and let it do the validation and the database saving based on just a few simple rules? Let's take a quick look at how that might work.

The easiest way to create a form ready for import is to use the WFFormCreator class that comes with Willowgarden. Here's an example of a form for entering a project task:

PHP:
  1. <?php
  2. $form = new WFFormCreator();
  3. echo $form->startForm("name: taskForm", "URL:", WFMakeURL('create', 'action'));
  4. $form->useNamePrefix("ProjectTask"); // this is important
  5. ?>
  6. <table>
  7. <tr>
  8. <td>Name of the Task:</td> <td><?php echo $form->newTextfield("name: name", "size: 30") ?></td>
  9. </tr>
  10. <tr>
  11. <td>Comments:</td> <td><?php echo $form->newTextarea("name: comments", "rows: 10", "cols: 60") ?></td>
  12. </tr>
  13. </table>
  14. <div><?php echo $form->newSubmit("title: Add This Task") ?></div>
  15. <?php
  16. echo $form->endForm();
  17. ?>

So what we're doing here is creating a form that will call the "create" action of the current page when the form is submitted. In the form, we output a textfield for the task name and a textarea for comments. But notice that line of code near the top that calls the useNamePrefix method of the form object? That sets up the form to use that prefix in the field name for each form element. So the actual name of the comments field isn't "comments" but is in fact "ProjectTask.comments". (Periods get translated to underscores in PHP, so in PHP you'd get "ProjectTask_comments" in your $_POST superglobal.) And of course, the name of the "name" field is in fact "ProjectTask.name".

Now in your page controller, you can write some very simple code to process the incoming form. Here's an example:

PHP:
  1. public function create_action()
  2. {
  3.    $task = new ProjectTask();
  4.    $task->importFromArray($_POST);
  5.    if (!$task->isValid()) {
  6.       $this->getRenderer()->assign("validationErrors", $task->getValidationErrors())
  7.        ->assign("task", $task)
  8.        ->render("add");
  9.    }
  10.    else {
  11.       $task->save();
  12.       WFRegistry::getRequest()->redirect("view/{$task->id}", "action");
  13.    }
  14. }

You can start to see where we're going with this. In the "create" action, we're instantiating a new ProjectTask object (a subclass of WFArchetype). Then we're importing data from the $_POST superglobal (which is by default filtered by the input filter that comes with Willowgarden to prevent Javascript and CSS hacks). The archetype is smart enough to check for the prefix of each form field to see if matches its own class name and import only those values. Then we're checking to see if the archetype is valid (i.e., all the form fields now stored in the archetype validate according to the archetype's validation rules) -- if it isn't, we'll obtain the validation errors and render the add form again; if it is, we'll save the archetype and redirect the browser to the view action with the new task's "id" as a path parameter.

Showing how to set up the ProjectTask archetype with database access and validation rules is beyond the scope of this article, but I hope now have a sense for how Willowgarden's Logic and Render layers (Controller and View layers in MVC parlance) work together very well to make your job as a Web developer much easier. One of the great things about the name prefix feature is that you can use multiple prefixes in the same form, allowing you to import the exact same form array into multiple archetype classes without causing any conflicts. You can also use prefixes for the id of form elements, which is a feature I'm using a lot in one of my applications.

And that's it for today's Willowgarden Highlight! Come back soon to learn more about this exciting new application platform for PHP 5.

Willowgarden Highlight: Template Modes

September 12th, 2006

Welcome to the first post in a series of posts I'll be writing about highlights of the Willowgarden application platform for PHP 5. This first highlight may be a simple one, but it's a very useful one as you will shortly find out.

Willowgarden, like many Web frameworks, is built around the concept of pages and actions. A page such as "photo" might have actions such as "view", "edit", "remove", etc. Some actions are visual and don't alter content, and some actions do alter content and aren't visual (they redirect to a visual action). The visual actions, of course, need to output HTML to the browser. How does that work? Well, as you might think, every page has a template associated with it, which you use via the page's built-in renderer to render the template. But, unlike many Web frameworks, Willowgarden doesn't provide a different template for every action. All actions are provided via a single template file for the entire page. This works because of the concept of template modes. (Note: for the purposes of this article, we're assuming we're using the default PHP-based template engine.)

Say someone wants to edit information for a photo. They'll go to a URL such as http://www.mysite.com/photos/edit/12345, which means the edit action of the photos page will be called and the first path parameter will be 12345. Now obviously you only want the HTML for the edit action to be rendered; you don't want the HTML for the view action or the process action or whatever to be rendered. So in the template file ("photo.php.tpl"), you would do this:

PHP:
  1. <?php
  2. if ($this->getMode() == "view"):
  3. ?>
  4. <p>Hello. This is the view action template.</p>
  5. <?php
  6. endif;
  7. if ($this->getMode() == "edit"):
  8. ?>
  9. <p>Hello. This is the edit action template.</p>
  10. <?php
  11. endif;
  12. // and so on and so forth
  13. ?>

Not bad, eh? Of course, the tradeoff of more files that are simple vs. fewer files that are slightly more complex might seem like a draw. But, and this is a big but, this template isn't in and of itself action-specific. So you can do tricks like this:

PHP:
  1. <?php
  2. if ($this->getMode() == "add" || $this->getMode() == "edit"):
  3. ?>
  4. Welcome to the <?php echo ucwords($this->getMode()) ?> area.
  5. <?php
  6. endif;
  7. ?>

Aha! Now we're handling two different template modes in one pass. We can have specific bits of the template for either action output different HTML based on the mode but keep much of the HTML the same. This is very helpful when we deal with "add" vs. "edit" -- 90% of the page content might be the same in either case. With sophisticated use of template modes, you can keep 90% of that HTML in one single place. Bottom line: managing your HTML templates just got a whole lot easier.

I hope you enjoyed my first Willowgarden Highlight. Come back soon for #2!

Developer Preview 2 of Willowgarden has been released today

September 8th, 2006

Willowgarden is an application platform/framework for PHP 5 that I started work on towards the end of August 2005, with serious development progress starting around the beginning of the year. On April 28, I released Developer Preview 1 of Willowgarden, and while that was a good start, I knew that the project still had a long way to go before it would resemble the level of quality and scope of functionality that it really deserved.

Well today, I'm so pleased to announce that Developer Preview 2 is ready. This is where the project is really starting to solidify and settle into a cohesive whole that can be used for hands-on development. I've been working on several Web site and applications using the DP2 codebase, and it has made my life as a PHP developer so much more fun than the bad old days of cobbling together scripts nearly from scratch.

Along with DP2, I've set up a new wiki that houses the partially-complete Users Guide as well as areas for future tutorials, how to's, and more. I'll be spending a lot of my time in the near future working on that end of things rather than code. You are most certainly welcome to contribute content to the wiki as well, so be sure to check that out.

And now I am going to make a bold statement: I am asking the cutting-edge PHP 5 developers reading this -- and I know you're out there! -- to consider joining the Willowgarden project if it piques your interest. I would like to start moving interim versions out there at a more rapid pace, leading towards a stable 1.0 release of Willowgarden, and to do that I need the help of people with many different environments and expertise in fine tuning installations. I also have a huge number of ideas in store for future versions, and I'm sure you do as well. This is the time to get on board while the project is still young! To contact me, feel free to send me an e-mail (remove everything after jared before the @ sign).

Thanks for reading, and I hope you download Willowgarden DP2 and give it a thorough look-over. This release is stable enough that I encourage you to start using it for your experimental development efforts, and your feedback and ideas are very much appreciated. Also, I'll be posting a lot here now about DP2, so check back soon for more information as well.

Willowgarden DP2 coming today!

September 8th, 2006

Hi folks -- just a quick note to let you know I'm on track to release Developer Preview 2 of the Willowgarden application platform for PHP 5 later today. Check back soon!

August 2006

Back in the Running

August 21st, 2006

Hey folks, I'm back! After being gone for a few weeks due to a family emergency, I'm now ready and rarin' to go. First on the agenda: getting Willowgarden Developer Preview 2 out the door! The first step in that direction is adding the code to a Subversion repository, and to that end I've put Willowgarden up as a project at Sourceforge.net.

So, the good news: the codebase for DP2 is now available via SVN and is roughly 95% complete! Yay! The bad news is that it'll be probably another week at least before I've worked enough on documentation, sample code, tutorials, etc. to feel comfortable building an official DP2 release. But if you like to be on the bleeding edge of tech, by all means go ahead with the SVN code. Please note: you'll need to read this post at the Willowgarden forums in order to set your environment up correctly.

In other news, xajax 0.5 is still on the table, and I'm hoping we can get a beta release out by late September. Keep your fingers crossed! Much more news on that to come in the days ahead.

Thanks for your patience, and I'll see you again here very soon!

Willowgarden DP2…preview of a preview?

August 2nd, 2006

I've changed my mind. See? That's what having an open mind will do for you! :) No, seriously, I have changed my mind, and what I'm referring to is the release plans for DP2.

I now realize I was too ambitious. I tried to "pull a Vista" (sorry Microsoft) and come out with a new release with practically everything new, improved, re-engineered, etc. Like Vista, it has resulted in a long delay and a lapse of immediate functionality improvements for interested users. Unlike Vista, I'm actually getting close to done with what I wanted to accomplish, and I'm pretty happy with the result.

So, as I said, my plans have changed. What I'm working on right now is cleaning up my current codebase enough to release something of use on Sourceforge right away. I hope to have a working Subversion repository up by early next week containing what will soon be known as the official DP2 release. Most of what needs to be accomplished before DP2 is formally unveiled is related to documentation, Web site changes, tutorial code, etc., so I figure in the meantime the real core code should be made available to the public for the cutting-edge developers out there to poke at.

Like I said, by early next week, you should be able to check out a real working copy of the DP2 codebase. If you're just a PHP developer dude or gal who wants to slap a Web site together with a framework in a couple of weeks, don't touch this with a ten-foot pole. But if you're an advanced PHP whiz who wants to discover the latest and greatest and help to make it even better, give it a try!

June 2006

Status update on xajax and Willowgarden

June 20th, 2006

I know I promised to post something last week, but I was just feeling too crummy. However, I'm much better this week and getting back into the swing of things. Man, I feel like most of June just completely disappeared on me.

Anyway, I want to give you all a status update on the two big PHP projects I've been working on, xajax and Willowgarden. First off, however, I want to thank my day-job employer, Commandtext, for allowing me to spend some time working on these projects during business hours.

xajax -- I was really getting down and dirty with 0.5 until a few weeks ago, but what I did get done was lay down on "paper" a good foundation for the new modular architecture. The first programming stabs at that were checked into SVN, so I hope to pick up where I left off shortly. Big thanks goes to Eion Robb (BigBrownChunx) who's been enhancing and refining the 0.5 codebase in my absence. It's good to have him on the project.

While xajax 0.5 will have a somewhat different (and IMHO better) API and will not be backwards-compatible with software currently using xajax 0.2.4, we're moving forward with the idea of a "legacy" layer that will allow you to upgrade to 0.5 without having to rewrite all of your code. You might have to make a very few changes just to use new class names, but that's about it I hope. More information about how this will work is forthcoming.

Willowgarden -- In terms of Willowgarden's public face, I didn't get the momentum going that I wanted to for this, and at the moment it seems a little bit like abandonware. In reality, I've been significantly improving and enhancing the base architecture on almost every level. I'm feeling really, REALLY good about the direction of this project now, and I'm looking forward to releasing Developer Preview 2. The release date for DP2 won't be this month as I had hoped. With luck, it'll be ready by this time next month.

Some of the main highlights of DP2:

  1. The Data Access layer, which includes the data mapper infrastructure, has been almost completely redone. Before, it was just a slight abstraction of a database layer. Now it's a full-fledged, storage-independent layer that is highly modular and easily customizable and gives archetypes (data/domain objects) ActiveRecord-style functionality using a clean API.
  2. The configuration classes have been enhanced to use a "best-pratice" XML format that distinguishes between framework-specific settings and custom settings via namespaces. Required and optional (with default) settings can be specified via custom subclasses. I honestly believe this new config system is now one of the best of its kind in the PHP world.
  3. Framework-wide access to logging and session management is now handled by simple interfaces which can be implemented by third-party libraries. PEAR's HTTP_Session2 is included by default for sessions. For logging, you can easily use PEAR_Log, Zend_Log, or another library (or your own).
  4. Due to the improved Data Access layer, archetypes are now very "smart" when it comes to data schema, relationships, validation, filters, etc. You can specify all this information programmatically using a cool "fluent interface" via the power of PHP 5. It's the perfect blend between the Zen-like abstraction of Ruby and the hands-on pragmatism of PHP.

You're going to love DP2. I'm just plain excited about it -- the level of detail is far greater than with DP1 which still had a lot of "holes" in it. Obviously, some aspects of DP2 will still be under construction, but more of the platform will be heading towards stable APIs and stable code, which is always a good thing. The Wiki/documentation and tests won't be fully baked yet, but something will be up and running when DP2 is released.

As always, check back here for the latest news. In closing, I want to reassure you that I've invested a lot of effort into both xajax and Willowgarden and will continue to do so for the foreseeable future. Jared is here to stay. :)

May 2006

Working on xajax 0.5 and Willowgarden DP2 in parallel

May 8th, 2006

Whew, I'm feeling a bit overwhelmed with the amount of work I have ahead of me for xajax 0.5 and Willowgarden DP2. Not that it's particularly difficult or time-consuming compared to the work I've already done, but in both cases the work requires a singularly focused and specific train of thought, which is tricky to keep up for long.

In the case of xajax 0.5, we're looking at the most substantial and architecture-altering upgrade in the entire history of the project. xajax is nearly a year old, and I jumped on board around October 2005, so it was already getting established when I started working on improvements. The latest release, 0.2.4, is a fine product, but I feel like the true potential of xajax has only just begun. I want 0.5 to provide an incredible platform for a huge ecosystem of innovation around the Ajax concept, and it's going to take a huge amount of design precision to make that happen. In particular, xajax is going to be retrofitted to work around a hot-swappable plugin architecture, so it will be possible to extend it completely in three major areas: the Javascript client side, the server request processing side, and the response command output side. It'll be amazing if done right, but a train wreck if done poorly, so there's a lot riding on the outcome of this endeavor.

As for Willowgarden, Developer Preview 1 was a decent first start, but there's a ton of work still to do -- a lot of which isn't even code related. I need to get the wiki up and running and start writing How To's and the beginnings of a real User's Guide. And on the code side, I want to have a fair amount of unit tests in place for DP2, as well as some additional sample apps to play with.

Hey, it's all fun. Otherwise, why am I doing all this?!?! :)

Willowgarden & Zend Framework at the same time!

May 4th, 2006

Hey, I'm back after taking a couple of days off (aaahhhhh....), and I'm about to embark on a strange new adventure: I shall attempt to discover that far-flung, exotic locale where I hear told that the rare Zend Framework lives underneath the shady canopy of the Willowgarden. I will make copious notes as I conduct my search and will report my findings shortly. Over and out.

(P. S. I've also started playing around a bit with Ruby on Rails just so, you know, I keep my resume current and all that. Yeah, that's it.)

April 2006

What is Willowgarden? Day 0: Launch

April 28th, 2006

Well, I'm pleased and extremely excited to tell you that Willowgarden is now online and ready for download. What is Willowgarden? It is a new rapid platform for bringing the next wave of Web applications to life using the power of PHP 5. It gives you the tools you need to design and develop great Web sites using an elegant optimized environment, yet you can easily tailor that environment to suit your particular and diverse needs.

Find out more by visiting the brand new Web site at http://www.willowgarden.org

and the Willowgarden Community Forums at http://forums.willowgarden.org

I'll be posting a lot more about Willowgarden in the days and months ahead right here at The Idea Basket, so subscribe to a news feed or check back here soon. I look forward to seeing how the project progresses; the future is bright!