Case-insensitive module URLs in Zend Framework routes


I started creating modules for my application using the CamelCase module names, as I interpreted the standard ZF syntax (ex. /path/to/app/root/application/modules/User/controllers/).  Then I realised this made my urls case-sensitiveas well (ex., http://www.example.com/User/).

Uppercase/CamelCase module names still make more sense to me, since the class names inherit the case.  If I used lowercase module names (ex., modules/user/) then my classnames would also be lowercase (ex., user_Model_Name) which really seems funny to me.

So I opted to make a front controller plugin to handle the conversion for me, thus making module names in the URL inherently case-insensitive.

/path/to/app/root/application/plugins/Init.php

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
/**
 * Init methods for Zend Controller
 *
 * @author gabrielk
 */
class Application_Plugin_Init extends Zend_Controller_Plugin_Abstract
{
    /**
     * Route startup hook
     *
     * @param  Zend_Controller_Request_Abstract $request
     * @return void
     */
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        $moduleName = $request->getModuleName();
        if ( 'default' !== $moduleName ) {
            $moduleName = $this->toCamelCase($moduleName, true);
            $request->setModuleName($moduleName);
        }
        return $request;
    }
    /**
     * Translates a string with underscores into camel case
     * (e.g. first_name -> firstName)
     *
     * @param string $str String in underscore format
     * @param bool $capitalise_first_char if true, capitalise the first char
     *                                        in $str
     * @return string $str translated into camel case
     */
    public static function toCamelCase($str, $capitalise_first_char = false) {
        if($capitalise_first_char) {
            $str[0] = strtoupper($str[0]);
        }
        $func = create_function('$c', 'return strtoupper($c[1]);');
        return preg_replace_callback('/_([a-z])/', $func, $str);
    }
}

In my real code, I use the static method Base::toCamelCase(); and have the toCamelCase method in my static app class.

Lastly, you need to register your plugin.  I do this in my Bootstrap.php file in an _init method.

/path/to/app/root/application/Bootstrap.php

1
2
3
4
5
protected function _initPlugins()
{
    $front = Zend_Controller_Front::getInstance();
    $front->registerPlugin(new Application_Plugin_Init(APPLICATION_ENV));
}

Thanks to Josh Ribakoff and Matthew Weier O’Phinney‘s articles for setting me in the right direction.

Copy from http://www.gabrielkoen.com/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s