July 21, 2006

Big News

New Release

A new tarball release of Friendly will be out in a couple of weeks. I’ve completely lost track of the 1.0dx series of numbers: the last “published” release is 1.0d1 (really 1.0d2), and I’ve made a couple dozen revisions since then, so we’ll call the next release 1.0e1 and start over.

New Features!

Among the recent changes:

A Rails-like url_for function (and Smarty helper). Friendly’s always had a function called url_for that dated back over a year, way before the initial developer release and even way before Friendly got coupled with Smarty. It never really got used because, well, PHP is not Rails. In Rails one can generate a URL to a controller or action using a hash (similar to PHP’s associative arrays), and due to Ruby’s beautifully succinct syntax it’s possible to write something like this:

url_for :controller => 'weblog', :action => 'post'

The PHP equivalent, sadly, is a little more arcane:

url_for(array('controller' => 'weblog', 'action' => 'post'));

But in Smarty — where I’d imagine this function will be getting the most play anyway — it gets a little bit cleaner:

{{url_for controller='weblog' action='post'}}

Until I took the hour or so to put together a url_for that made sense, I was having to create links like this:

<a href="{{$cfg.base_url}}admin/microsites/view/22">Some stuff</a>

And God help me if I had to have that trigger an Ajax action:

<a href="{{$cfg.base_url}}admin/microsites/view/22" onclick="new Ajax.Request(this.href,{asynchronous:true,method:'post'}); return false;">Some stuff</a>

The new url_for doesn’t completely solve all URL-related problems, but it at least makes generating portable links a little bit easier.

Database-backed sessions out of the box: We ran into a problem recently on a couple of sites where sessions were being lost after a few minutes (usually 30 minutes, but sometimes as little as five) because PHP on that particular shared box was stashing all the sessions — for every PHP/Friendly app on the machine — in /tmp. We tried for a little while to reconfigure PHP to hold onto the sessions, but we couldn’t get anything to work that wouldn’t have somehow interfered with the other 300 PHP scripts on that machine.

So I decided it was time to bring Friendly’s sessions support into the 21st century: in the current trunk version of Friendly all sessions are now stored in a MySQL table by default, and I plan to expand the session management features really soon to allow for Friendly-managed sessions to be stored on the filesystem or other database engines as well.

The main benefit here, of course, is that now Friendly keeps tabs on its own sessions and isn’t dependent on PHP or the host operating system for garbage collection. A side benefit, however, is speed: the first app I developed the DB sessions support for is now running noticeably faster now that it isn’t waiting for every other PHP script on the machine to finish doing its sessions thing to get started.

There are a few caveats/limitations to this new feature: The username Friendly uses to connect to the database (as specified in config/config.yml) must have the ability to create tables. In most shared environments you only get one MySQL user that has full privileges on a single database, and so this should be no problem. The reason for this is that rather than require a setup involving SQL files and/or shell commands, Friendly simply checks for the _friendly_sessions table and creates it if it doesn’t exist.

And the big limitation: Friendly’s session management features require that session_write_close() be called at the end of the request for any session data to be written. FriendlyMVC now calls this at the end of every request, and redirect_to calls it prior to redirecting and exiting the script. And as I find instances where a call to exit() is causing sessions not to be saved I’m going in and fixing them.

The three of you who’ve written Friendly apps already may be wondering: why on earth would I ever use exit in a Friendly app? Well, it comes in handy when you’re doing Ajax — if you don’t explicitly end the request in your action, Friendly will do its thing and apply the template and layout, which is often not what you want if you’re just sending back some updated data for a small part of the page.

So for now, be advised that you cannot use exit() without first calling session_write_close() unless you want any session changes made on that request to disappear into the ether. I acknowledge that that can really cramp your style, however, so I’ve got a solution: friendly_exit(), a convenience function that behaves identically to exit(), except that it makes sure the session is saved and performs whatever other cleanup may be required.

And this does remind me to mention: Friendly’s Ajax support is next on the list of things to get an overhaul, because the only thing better than this:

<a href="{{url_for action='view' id=$item->id}}" onclick="new Ajax.Request(this.href,{asynchronous:true,method:'post'}); return false;">Some stuff</a>

is this:

{{link_to_remote link_text='Some stuff' action='view' id=$item->id}}

New Server!

Since the launch of friendlyphp.org back in February we’ve been hosted on a shared box at TextDrive (except for the wiki, which lives at StikiPad), and that’s worked out pretty well. But things could be a little bit better. For example, we could stand to have a little more control over the Subversion repository, and a little more memory so we can run Collaboa or Trac without crashing somebody else’s Rails app.

So Jesse at Blue Box has generously offered us the use of one of his new performance VPS servers, and as soon as I have time I’ll be moving the Friendly project and ephemera over to it. For the first time there will be a ticketing system so you — yes, you! — can browse the source or, if you feel the urge, help contribute to Friendly by submitting a patch or two.

Look for more details in this space in about two weeks.

March 8, 2006

Just Added: Documentation Wiki

StikiPad looks kinda nice,” I said last week when asked if I’d had an opportunity to check it out, “but I f**king hate wikis.”

It’s true, I generally am opposed to wikis — I’m enough of a control freak that I prefer to have something that’s totally under my control. But I’ll say this about a wiki: it’s perfect for a living or in-progress document like the Friendly API docs. And I’ll say this about StikiPad: it’s a fantastic hosted wiki app — I’d go so far as to call it the wiki software for people who hate wikis. It’s got two features in particular that caught my eye:

  • Domain Mapping (so StikiPad-hosted wikis can appear to live on the same domain as the rest of one’s website)

  • Custom Templates using Liquid, a lovely little template engine not dissimilar from our own Smarty.

I’d been evaluating a couple of ways to go about building the API docs for Friendly, including a PHP-based solution called MediaWiki (the same stuff that powers Wikipedia) and (of course) just building a small CMS using Friendly. But MediaWiki doesn’t offer as much flexibility of design as I would like, and from what I hear it’s seriously resource-hungry. And building something would be a lovely example of the framework in action, but I’ve got some serious workload at the moment and building something from scratch would only serve to delay getting these API docs written and published.

So if you click on the Documentation tab of this site, you’ll find that it now points to the Friendly PHP Wiki, where there is actually some useful content. (Gasp!!!)

What’s there is pretty sparse, but I plan to continue adding to it as I work on Friendly, with a goal of having a more-or-less complete reference to the current API by the time I’m pushing 1.0d2 out the door in the next 3-4 weeks.

March 6, 2006

Friendly Hearts PHP 4

First off, sorry for the low frequency of updates to this blog and website. I promise: API docs are still forthcoming, and I’m planning to put together a second Developer Preview release later this month.

As I’ve mentioned in the About section, support for PHP 4 is one of the major features of Friendly. Most plain-vanilla shared hosting environments offer it, whereas the number of hosts that offer PHP 5 is still relatively small. Unfortunately, PHP 5’s support for object-oriented programming (OOP) — while still not nearly as nice and flexible as Ruby, Perl or even JavaScript for chrissakes — is a lot more advanced than PHP 4’s. And a lot of the things I would like to do with Friendly (like true Active Record-style domain models) that would be clunky-but-doable in PHP 5 are way more difficult, maybe even impossible, when you factor in PHP 4.

Continue reading "Friendly Hearts PHP 4" »

February 7, 2006

Say Hello to Friendly

Welcome to the website for Friendly, a simple, lightweight framework that makes developing PHP applications a lot simpler and less stressful. I’ve been developing Friendly over the past 8-9 months as a tool for situations when more advanced frameworks like Ruby on Rails were not an option. I could (and have) go on and on about the story and philosophy of Friendly, but for now I’m just eager to get this thing out the door and see what people think.

Friendly 1.0d1, our first public release, is stable and feature complete in the sense that everything needed for day-to-day application development under Friendly is present, although I’ve got a list of planned features a mile long that just didn’t make the cutoff date for this version. The documentation is a little sparse at the moment, but I’ve written a nice little Getting Started page that gives you the basics, and my goal is to have the beginnings of the API reference in place in the next week or so.

Friendly is a pretty sparse API, and that’s intentional: I wanted to clarify the 1,000+ functions of the PHP API into those few that I use on a regular basis, and extend them to make them more powerful. So you can certainly continue to use mysql_query() to talk to your database, but my hope is you’ll forget all about that once you’ve used Friendly’s find_by_sql().

I also made the conscious decision to support older versions of PHP as fully as the latest ones; while some other frameworks out there require PHP 5.x, Friendly will run just fine under PHP 4.2. That’s because Friendly was born of a need to inject some modern, MVC-style power into a hosting environment that couldn’t be configured to run Rails or Django. I feel pretty strongly that you shouldn’t have to learn a whole new set of tricks or upgrade to a more expensive (and usually harder-to-manage) hosting situation just to bring some clarity and power to your web development, so I’m committed to maintaining PHP 4 support for the forseeable future.

Right now MySQL is the only supported database, and originally I hadn’t planned to ever include support for anything else. However I know a lot of people are getting into PostgreSQL, and some developers may find themselves needing to integrate Friendly apps into an Oracle or Microsoft server environment at some point. So support for non-MySQL databases is on the way — I plan to have at least Postgres support in place before 1.0 Final. (And when will that be? Not sure yet, probably in late Spring.)

Friendly is open source software released under the MIT license. That license doesn’t require that you submit any changes you might make to the source code as patches, but contributing is certainly a neighborly thing to do and much appreciated. There’s not yet a proper mechanism in place for submitting a patch, but if you’ve got a suggestion or some fresh code, just send me an e-mail at the address in the next paragraph.

Yes, I know — there’s a lot missing from this site, like API documentation. But I’ll be working to fill in the gaps over the next couple of weeks. While end-user support is not really available for Friendly, I welcome questions and bug reports via e-mail (even though I can’t promise to be able to respond right away), at info@friendlyphp.org. Developer forums are also on the way real soon.