-
Horde/Routes 0.4 Released Mar 4, 2009
Horde/Routes 0.4 has been released. Horde/Routes is a URL mapping system for PHP 5 that provides classes for mapping URLs to the controllers and actions of an MVC system. Learn more at the project website or this presentation.
This version includes a new utility class,
Horde_Routes_Printer
, a debugging aid that can print out a summary of all routes that are connected to a mapper. It is available through a convenience method fromHorde_Routes_Utils
:$map = new Horde_Routes_Mapper(); $map->connect('post', ':title', array('controller'=>'posts','action'=>'show')); $map->connect('feeds', 'feeds/:format', array('controller'=>'feeds')); $map->connect(':controller/:action/:id'); $map->utils->printRoutes();
This will cause the following information to be printed to
php://output
:post /:title {:controller=>"posts", :action=>"show"} feeds /feeds/:format {:controller=>"feeds", :action=>"index"} /:controller/:action/:id
You’ll see above that the name of the route (if any) is shown, the route path, and the default values for the route. If a route only matches for a certain HTTP method, this will be shown as well. It’s handy when you have a large number of routes, or resource routes.
If you’d like the debug output to go somewhere else, pass a stream resource as the first argument to
printRoutes()
. The output is formatted similarly to therake routes
command from Rails. Although the notation is a little unusual for PHP, it’s compact and readable. SeeHorde_Routes_Printer
if you’d like to modify the format.Since it’s release, we’re received a good amount of positive feedback on Horde/Routes. It is now being shared by the upcoming Horde 4 (Rampage), the Seagull framework, and a few others. Check out the integration guide for how to add it to your project.
-
PyWorks 2008 Slides Nov 15, 2008
Slides from my PyWorks 2008 talks are now available. This conference was shared with php|works and I enjoyed this format. It was great to see many of my Python and PHP friends at the same event. Thanks to everyone who attended my talks.
Py65: Microcontroller Simulation with Python
This talk introduced the venerable 6502 microprocessor family, building small computer systems with these parts, and then simulating those systems with Py65.
The audience participation was great. We had fun stepping through some small assembly language programs on the simulator. One attendee wrote:
This was fascinating and the speaker was awesomely enthusiastic. The overview of microcontrollers and their significance was enlightening and entertaining. The simulator design presented was fantastically simple and very Pythonic. I can’t wait to see where this project goes.
Thanks and I’m glad you enjoyed it. For updates on the Py65 simulator, please watch my blog and the Py65 project page on Ohloh.
URL Mapping with Routes
We explored the Routes library from the ground up, setting it up and then exploring its options and matching. We worked through many of the examples with live demos on the Python interactive interpreter.
The talk was attended by several Pylons users, who gained a better understanding of how Routes works by seeing it outside the context of any particular web framework.
URL Mapping with Horde/Routes
Bonus Slides! Horde/Routes is a PHP 5 library that is a direct port of Routes. Since there were so many PHP folks at this conference as well, I ported all of the examples in my Routes talk to work with Horde/Routes.
These slides will help you get acquainted with the PHP version. Since the two presentations are otherwise identical, you might also find it an interesting comparison between Python and PHP 5.
-
PHP Temporary Streams Oct 17, 2008
It’s been a while since David Sklar called out to let a thousand string concatenations bloom. That discussion produced some entertaining suggestions for putting strings together such as using
preg_replace
and calling out to MySQL withSELECT CONCAT
.Here’s an approach that uses filesystem functions. When combined with some lesser-known PHP streams functionality, it has several practical applications.
Opening Files for Reading and Writing
There are several modes for opening a file that will allow you to seek to arbitrary positions in the file and read or write at those positions. One of the most frequently used of these modes is
w+
, which thetmpfile
function uses automatically.We can repeatedly write, then rewind the pointer and read:
$f = tmpfile(); fwrite($f, 'foo'); fwrite($f, 'bar'); rewind($f); $contents = stream_get_contents($f); //=> "foobar" fclose($f);
When writing to the filesystem, the above provides yet another inefficient solution to David’s exercise. Now let’s take it a bit further to see how this can be useful.
In-Memory Streams
PHP 5.1 introduced two new in-memory streams:
php://memory
andphp://temp
. Thephp://memory
stream operates entirely in memory. Thephp://temp
stream operates in memory until it reaches a given size, then transparently switches to the filesystem.We can modify the above example to use the
php://memory
stream instead of hitting the filesystem:$f = fopen('php://memory', 'w+'); fwrite($f, 'foo'); fwrite($f, 'bar'); rewind($f); $contents = stream_get_contents($f); //=> "foobar" fclose($f);
Putting a string inside a fast temporary stream can be very useful. For example, we can then attach filters to that stream.
Testing
Temporary streams are also handy for testing. There are some rather elaborate virtual file system libraries out there but many times a stream is all you need.
Zend_Log has a log handler for streams that accepts either a filename or a stream resource. We can configure it with a
php://memory
stream for testing:$f = fopen('php://memory', 'w+'); $writer = new Zend_Log_Writer_Stream($f); $logger = new Zend_Log($writer);
Assuming your well-designed application has a convenient injection point for the logger instance, your test can pass it in before your test activates some action which should result in logging:
$logger->crit('critical message worth testing');
When the action has completed, the test can rewind
$f
and use it as a test spy.rewind($f); $contents = stream_get_contents($f); $this->assertRegExp('/message worth testing/i', $contents); // PHPUnit
Not surprisingly, my unit tests for Zend_Log use this same technique.
Application Usage
Not long ago, we built a custom document storage system for a client. One of its more interesting features is that it integrates with an internet fax service so users can select documents in the system and then fax them. For each fax, the software automatically generates a cover page.
To make the cover page, I first made a nice template using a drawing tool and then saved it in PDF format. I then used Zend_Pdf to write over the template with dynamic content. I first demonstrated this technique in this article.
Since the cover page is only used once (during transmission) and easy to regenerate, I don’t save the output to the filesystem. Instead, I create a
php://temp
stream. The instance methodZend_Pdf->render()
writes the PDF output only to that stream, which is then rewound. Functions likestream_copy_to_stream
orfpassthru
can then be used to send the final output where it needs to go, and the whole process normally never needs to use the disk. -
PHP Developer Best Practices Sep 16, 2008
Matthew Weier O’Phinney and I gave a tutorial session at ZendCon 2008 this year titled “PHP Developer Best Practices”. The tutorial touched on source control, coding standards, testing, documentation, and more.
The slides are now available in PDF format.
We were located in Hall B of the Santa Clara Convention Center, which is a very large room that’s also used for the keynotes. Andi told us the room was selected based on the number of people who registered for our session. We initially had doubts but the attendance was greater than any previous year and the room worked out quite well. We were thankful that unlike last year, everyone was able to get a seat.
Thank you to all who attended. We enjoyed meeting many of you during the breaks and hope that you found our session helpful.
-
Speaking at PyWorks 2008 Aug 29, 2008
PyWorks is a new conference from the folks at Python Magazine. It is being co-hosted with a PHP conference, php|works, and attendees of either conference have access to talks on both the Python and PHP tracks. Being a user of both languages, I think this a great idea and I’m looking forward to this format. I’ll be giving two talks at PyWorks 2008:
Microcontroller Simulation in Python
This talk will present Py65, my open-source simulation of a small microcomputer system based on the MOS 6502. The 6502 is a very famous microprocessor that was used in early microcomputers like the Commodore 64, but its design has stood up for over 30 years. Cores based on the 6502 are now widely used in embedded devices, with WDC estimating annual volumes in the hundreds of millions of units.
Using Python and software tools such as Py65, low-level software for embedded systems can be developed and tested much faster. This talk will discuss the design and implementation of Py65, how it and tools like it can help, and will also touch on other Python libraries that can assist with embedded development. (PyWorks Abstract)
URL Mapping with Routes
Routes is a Python package that provides a solution to the problem, “how do I map URLs to my code?” Its solution is an interesting one, and is actually a re-implementation of a feature from Ruby on Rails. Routes itself has also now been ported to PHP 5 as part of the Horde project. Routes is used by the Pylons web framework and other frameworks in Python, and is relatively easy to use as a standalone package.
The Routes method of URL dispatch is based around pattern matching rather than object publishing. For those new to Routes, we’ll have an introduction to the basic Routes concepts and how it works. We’ll also dive into the Routes internals and follow some URLs through their recognition phase, learning about how Routes does it job along the way. Web developers and framework implementers alike will gain a better understanding of Routes and how to use it effectively. (PyWorks Abstract)
-
Speaking at ZendCon 2008 Aug 22, 2008
I will be speaking again this year at the Zend/PHP Conference. I have teamed up with Matthew Weier-O’Phinney to present PHP Developer Best Practices:
During this tutorial session, we will cover a number of best practices you can institute in your organization or in your personal coding toolbox, including:
- Testing Strategies
- Coding Standards
- Documentation
- Version Control
- Deployment Methodologies
We will discuss the hows and whys of each topic, and strive to cover concrete examples that you can start implementing immediately, so that you and your developers can start working more efficiently.
It has become a tradition for Matthew and I to present a tutorial session together at ZendCon. We’ve done a new one every year since the first ZendCon. I always enjoy this conference and I’m looking forward to it again this year.
Update: Slides from the presentation are now available.
-
OSCON 2008 Slides Jul 25, 2008
Slides from my two talks at OSCON 2008 are now available:
Both talks were well attended had great audience participation. Thanks to everyone that attended and I hope you enjoyed them.
About seven people came up after the Integration Testing talk with good questions and feedback. I think that 45 minutes was enough to provide good starting points for the topics covered but it was clear that these users were engaged and ready to dig into it more. If I give this talk again, it is going to be in a tutorial format so we can get much deeper into the material and have some exercises.
After my Supervisor talk at the end of the day, I had the pleasure of going out to dinner with Roger Hoover and some other Supervisor users. It’s been very exciting to see Supervisor picking up traction over the past year. Supervisor is being used in several large architectures of companies whose names you know. If you’re using Supervisor and wouldn’t mind us telling others about it, please contact me.
-
Horde/Routes 0.3 Released Jun 15, 2008
Horde/Routes is a URL mapping system for PHP 5. It is a direct port of the Routes, a Python library that is part of the Pylons project. Horde/Routes is a standalone library designed to be integrated into any MVC framework.
This release brings in some changes from Routes 1.8. The most noticeable change is that custom actions on RESTful routes are now delimited with the forward slash (
/
) instead of the semicolon (;
). This was done for parity with Ruby on Rails.I have also fixed some bugs, notably that the resource route generator failed to generate routes that recognized
PUT
andDELETE
requests on “formatted” resources (/messages/1.xml
). This fix will be merged upstream to the Python version.I am also using the Python version and I met with Ben Bangert, the author of the Python version, at PyCon 2008. Each release of Horde/Routes has resulted in patches to the Python version. It’s nice how this small ecosystem has developed around the routes concept between these projects (Ruby on Rails, Pylons, and our work in Horde).
Since Horde/Routes 0.3, the default RESTful routes generated by Horde/Routes are fully compatible with the latest version of ActiveResource. We have a new project at work that is using Horde/Routes and ActiveResource together and it works well.
-
Deploying on Phusion Passenger May 13, 2008
A very large number of PHP developers, perhaps even the majority, are building smaller web applications. These applications receive only a moderate amount of traffic and usually have a single database server, often on the same machine.
Deployment, or moving your application to production for use by real customers, is largely an afterthought for these small PHP applications. In many cases, decent PHP code can just be installed on the server and it runs without much trouble.
One of the points we stress towards the back of our book is that deploying Rails applications can be more difficult than deploying their PHP counterparts. There are more moving parts and things to you’ll need to learn. Until you get the hang of it, deploying even small Rails applications can be frustrating.
A recent development has greatly improved this situation.
Introducing Phusion Passenger
PHP can be deployed using a variety of server configurations. Some of these can be just as frustrating as traditional Rails deployments. However, the majority of PHP applications are still deployed on Apache using
mod_php
. PHP’s tight integration with Apache is simple, proven, and just works for many needs.Phusion is a small company in the Netherlands that recently released an open source product called Passenger (
mod_rails
). Passenger aims to take the complexity out of deploying Rails applications by also integrating with Apache.Installing Passenger on the average Linux or Mac server is usually simple. It is installed as a gem and contains an automated installer program that compiles and installs the necessary components. Once installed, many Rails applications can be deployed under Apache simply by configuring a VirtualHost for each application. The installer even outputs an example for you to copy and paste.
Passenger gives a deployment experience closer to what we’ve come to appreciate with PHP. In the background, there’s still more moving parts, but Passenger automatically manages them. It spawns Rails application server processes, proxies to them, and largely eliminates the configuration and glue that other Rails deployment options leave up to you. Passenger has nice documentation for when some configuration is necessary.
At work, we develop applications in both PHP and Ruby. We’ve been testing Passenger for a few weeks on a dedicated server and recently deployed an application on it. We have been very impressed with its reliability, performance, and easy-of-use compared to previous Rails deployment options. You should give Passenger strong consideration, especially if you’re just starting out.
Finally, Rails on Shared Hosting
Traditionally, deploying Rails applications on shared hosts (usually with FastCGI) has been difficult and unreliable. Due to this, many Rails applications are deployed on small VPS plans from providers like Rimuhosting and Slicehost. These run well, typically using small Mongrel cluster behind a frontend proxy balancer. However, VPS plans are usually more expensive and more work to set up and maintain than shared hosting.
Passenger makes running Rails applications on shared hosting much better. Dreamhost, a major shared hosting provider, recently announced full support for Passenger. We tested a small Rails application on Dreamhost using it and we were impressed. It seemed to run without issues and with reasonable performance.
Deploying some Rails applications on shared hosting may soon become commonplace.
Thinking Ahead
Will better Rails deployment options or Rails on shared hosting mean it’s time to switch from PHP? No. It’s unlikely this blog will switch from WordPress anytime soon and Phusion’s own blog even runs on WordPress. There are many great PHP applications out there. More importantly, sometimes PHP will simply be a more appropriate solution.
PHP, Ruby, and Rails are all great tools you can choose from. You’ll still need to learn these tools, their relative strengths, and when it’s appropriate to apply each.
Passenger just means Rails deployment is getting simpler and more mature. It also means deploying PHP and Rails applications together on the same Apache instance is now much easier. These are more reasons to consider adding Rails to your toolbox. It’s certainly a great time to be building web applications.
-
Speaking at OSCON 2008 Apr 2, 2008
The OSCON 2008 website has published its talk schedule. I’ll be giving two talks at OSCON this year; one on the Python track and one on the PHP track.
Supervisor as a Platform
I will quickly introduce you to Supervisor and the immediate benefits of running your server processes under it. We will then dive into how applications written specifically for Supervisor can take advantage of it as a platform — writing your own event listeners to observe Supervisor and the process lifecycle, controlling with XML-RPC, and extending the Supervisor core with your own Python extensions.
This will be an expanded version of the talk given at PyCon 2008. Since PyCon, there’s been quite a few interesting developments in Supervisor like the ability to extend
supervisorctl
and progress made on configuration reloading. We’ll touch on these as well, so if you attended the PyCon talk there will still be new and interesting material in this talk for you.Integration Testing PHP Applications
While more PHP developers are recognizing the importance and benefits of unit testing, the uptake of PHP developers using automated integration or acceptance testing is relatively slow. This testing is equally crucial to maintaining the integrity of applications.
I’m going to help get you started testing at the application level with practical tips and source code. We’ll look at how to structure your HTML markup so it’s more easily testable, making tests easier to write and maintain with CSS selectors, organizing your tests, and testing with or without a browser.
I’d also suggest you check out Sebastian Bergmann’s tutorial session on PHPUnit’s integration with Selenium RC.