Archive for August 2008

Quickie: Module-specific Error Controllers in Zend Framework (1.5)

In my quest to do some alterations on ZF error handling (in particular, render the view if the action or controller is not found, makes it real easy for my designer to prototype) I had the desire to be able to allow modules to have their own ErrorControllers. Unfortunately, the Zend_Controller_Plugin_ErrorHandler() default does not allow for this and I didn't really want to extend that class (I planned on handling the rendering in the ErrorControllers) so I wrote up a quick plugin. Doing random stuff like this has really helped me get to know the Zend Framework (I was developing my own internal framework architectured somewhat similarly that I have abandoned due to time constraints and the large community behind ZF).

The plugin hooks into the routeShutdown as this is the first place I have access to the ZF-determined module name. I need the current module name to make sure I override the ErrorHandler's module, and while I could determine that for myself, problems would arise if I decided to start overriding the routers and URL structure.

<?php
class MyApp_ControllerPlugin_ErrorControllerSelector extends Zend_Controller_Plugin_Abstract
{
	public function routeShutdown(Zend_Controller_Request_Abstract $request)
	{
		$front = Zend_Controller_Front::getInstance();
 
		//If the ErrorHandler plugin is not registered, bail out
		if( !($front->getPlugin('Zend_Controller_Plugin_ErrorHandler') instanceOf Zend_Controller_Plugin_ErrorHandler) )
			return;
 
		$error = $front->getPlugin('Zend_Controller_Plugin_ErrorHandler');
 
		//Generate a test request to use to determine if the error controller in our module exists
		$testRequest = new Zend_Controller_Request_HTTP();
		$testRequest->setModuleName($request->getModuleName())
		            ->setControllerName($error->getErrorHandlerController())
		            ->setActionName($error->getErrorHandlerAction());
 
		//Does the controller even exist?
		if( $front->getDispatcher()->isDispatchable($testRequest) )
		{
			$error->setErrorHandlerModule($request->getModuleName());
		}
	}
}
 
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin( new MyApp_ControllerPlugin_ErrorControllerSelector() );
?>

You'll notice I generated a Zend_Controller_Request_HTTP() object and filled it with the ErrorController's set controller name and action name (again if I decided to change how the error controllers are named, this plug in will still work) then use the dispatcher to check if this controller exists (basically, can we dispatch this controller's action from this module). If so, go ahead and set the ErrorHandler's module name to the current module name.

I'll bring more later once I finish making the Zend Framework my bitch.

End Of An Era (Goodbye to PHP 4)

PHP 4's end-of-life is today.

With that I bid adieu to the half-assed OOP support and general primitive nature of the platform.

However I cannot be so callous as my first forays into PHP were with 4.0 releases...

Now the question is how much faster will the dumping of PHP 4 (and 5.0/5.1) be and the adoption of PHP 5.2? Granted most major services are already at the latest/later versions of PHP 5.2 (5.2.5 seems to be most common, but quite a few have 5.2.6 installed), I hope the prevalence extends even further as I'm writing an app that requires 5.2.3+ (damn you Doctrine and your shiny ways!)...

Displaying N-Deep Trees (Remember Your Algorithms Course?)

Often times when doing web development one has the need to categorize things. And not only basic categorization, n-deep hierarchical categorization. I've already discussed storage and retrieval of such data, but there comes a time when one needs to display this information.

Sometimes people build systems to only account for their current requirements. If the software calls for only 2 levels of categorization (Parent and Child only), a simple nested for loop will suffice. However, software requirements change and you'll soon find yourself up shit creek without a paddle if you need to support 3 or 4 levels of nesting.

For those of us who have formal computer science training, the answer comes rather quickly. To those who's training is less formal (most web developers I meet have practical training, not formal), I'll help you out: Tree Traversals (or if you are completely lost, Recursion). If you know absolutely nothing about recursion, I would suggest you familiarize yourself now. A recent, easy to read example can be found here.

Continue reading ‘Displaying N-Deep Trees (Remember Your Algorithms Course?)’ »