License for code published on this site

25th June 2008

A few people have asked me, so I thought I'd better make it explicit.

All non-trivial code on this site is released under the New BSD license as noted here.

For code examples that are a line or two long, I consider them added to the public domain.

UK Readers: Don’t buy petrol from BP or ESSO

6th June 2008

This is very off-topic, so feel free to skip!

Received a round-robin today via email. As I don't send on such things, I thought I'd mention it here as I'm getting fed up with the price of petrol, especially given the profit that the big oil companies have recently announced:

Received from a good friend so leave the rest to you!

See what you think and pass it on if you agree with it.

We are hitting £123.9 a litre in some areas now, soon we will be faced with paying £2.00 a ltr. Philip Hollsworth offered this good idea:

This makes MUCH MORE SENSE than the 'don't buy petrol on a certain day' campaign that was going around last April or May! The oil companies just laughed at that because they knew we wouldn't continue to hurt ourselves by refusing to buy petrol. It was more of an inconvenience to us than it was a problem for them. BUT, whoever thought of this idea, has come up with a plan that can really work.

Please read it and join in!

Now that the oil companies and the OPEC nations have conditioned us to think that the cost of a litre is CHEAP, we need to take aggressive action to teach them that BUYERS control the market place not sellers. With the price of petrol going up more each day, we consumers need to take action. The only way we are going to see the price of petrol come down is if we hit someone in the pocket by not purchasing their Petrol! And we can do that WITHOUT hurting ourselves. Here's the idea:

For the rest of this year DON'T purchase ANY petrol from the two biggest oil companies (which now are one), ESSO and BP.

If they are not selling any petrol, they will be inclined to reduce their prices. If they reduce their prices, the other companies will have to follow suit. But to have an impact we need to reach literally millions of Esso and BP petrol buyers. It's really simple to do!!

…(snip boring bit)…

If this makes sense to you, please pass this message on.

PLEASE HOLD OUT UNTIL THEY LOWER THEIR PRICES TO THE 69p a LITRE RANGE

It's easy to make this happen. Just forward this email, and buy your petrol at Shell, Asda, Tesco, Sainsburys, Morrisons, Jet etc. i.e. boycott BP and Esso

I'll write about something a bit more on topic soon!

Zend Framework URLs without mod_rewrite

3rd June 2008

Some of our Zend Framework applications have to run on IIS without ISAPI_Rewrite installed. In these cases we need urls of the form http://www.example.com/index.php?module=mod&controller=con&action=act. I couldn't get this to work out of the box with Zend Framework 1.5, so wrote my own router called App_Controller_Router_Route_RequestVars.

This code obviously only supports what I needed and I've only tested it on IIS for Windows 2003 Server, so you may need to tweak to make it do what you want! Feel free to share any fixes :)

This is the code:

<?php

/** Zend_Controller_Router_Exception */
require_once 'Zend/Controller/Router/Exception.php';

/** Zend_Controller_Router_Route_Interface */
require_once 'Zend/Controller/Router/Route/Interface.php';

/**
 * Route
 *
 * @package    App_Controller
 * @subpackage Router
 * @copyright  Copyright (c) 2008 Rob Allen (rob@akrabat.com)
 */
class App_Controller_Router_Route_RequestVars implements Zend_Controller_Router_Route_Interface
{
    protected $_current = array();

    /**
     * Instantiates route based on passed Zend_Config structure
     */
    public static function getInstance(Zend_Config $config)
    {
        return new self();
    }

    /**
     * Matches a user submitted path with a previously defined route.
     * Assigns and returns an array of defaults on a successful match.
     *
     * @param string Path used to match against this routing map
     * @return array|false An array of assigned values or a false on a mismatch
     */
    public function match($path)
    {
        $frontController Zend_Controller_Front::getInstance();
        $request $frontController->getRequest();
        /* @var $request Zend_Controller_Request_Http */
        
        $baseUrl $request->getBaseUrl();
        if (strpos($baseUrl'index.php') !== false) {
            $url str_replace('index.php'"$baseUrl);
            $request->setBaseUrl($url);
        }
        
        $params $request->getParams();
        
        if (array_key_exists('module'$params)
                || array_key_exists('controller'$params)
                || array_key_exists('action'$params)) {
            
            $module $request->getParam('module'$frontController->getDefaultModule());
            $controller $request->getParam('controller'$frontController->getDefaultControllerName());
            $action $request->getParam('action'$frontController->getDefaultAction());

            $result = array('module' => $module, 
                'controller' => $controller, 
                'action' => $action, 
                );
            $this->_current $result;
            return $result;
        }
        return false;
    }

    /**
     * Assembles a URL path defined by this route
     *
     * @param array An array of variable and value pairs used as parameters
     * @return string Route path with user submitted parameters
     */
    public function assemble($data = array(), $reset=false)
    {
        $frontController Zend_Controller_Front::getInstance();
        
        if(!array_key_exists('module'$data) && !$reset 
            && array_key_exists('module'$this->_current)
            && $this->_current['module'] != $frontController->getDefaultModule()) {
            $data array_merge(array('module'=>$this->_current['module']), $data);
        }
        if(!array_key_exists('controller'$data) && !$reset 
            && array_key_exists('controller'$this->_current) 
            && $this->_current['controller'] != $frontController->getDefaultControllerName()) {
            $data array_merge(array('controller'=>$this->_current['controller']), $data);
        }
        if(!array_key_exists('action'$data) && !$reset 
            && array_key_exists('action'$this->_current)
            && $this->_current['action'] != $frontController->getDefaultAction()) {
            $data array_merge(array('action'=>$this->_current['action']), $data);
        }
        
        $url ";
        if(!empty($data)) {
            $urlParts = array();
            foreach($data as $key=>$value) {
                $urlParts[] = $key '=' $value;
            }
            $url '?' implode('&'$urlParts);
        }

        return $url;
    }
}

This route is then added to the Front Controller's router in my bootstrap like this:


$frontController Zend_Controller_Front::getInstance();
$router $frontController->getRouter();
$router->addRoute('requestVars', new App_Controller_Router_Route_RequestVars());

Hopefully this is a useful starting point for others who can't use mod_rewrite with Zend Framework.