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-sensitive as 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

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

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.

Advertisements

5 thoughts on “Case-insensitive module URLs in Zend Framework routes

  1. or maybe you can use built-in filter Zend_Filter::filterStatic() to do that, so it would be :

    $moduleName = Zend_Filter::filterStatic($moduleName, ‘Word_UnderscoreToCamelCase’);

    $request->setModuleName($moduleName);

    Like

  2. Sorry to interrupt
    Could you just tell me how to use Register Plus Redux plugin? I am totally confused as even the author's site does not discuss. How to bring in on to the site? I used all settings but of no avail.
    (I saw your comment on Pie Register Plugin, in favour of Register Plus Redux and thought writing to you. On your site also there is no contact button, hence I came here)

    Like

    1. If a plugin isn't doing what you think it should be doing, first make sure you're running the latest version of WordPress, then use the plugin with the default WordPress theme.

      If it works with the default theme, then the problem is with something in the theme you're using (probably a template hook that's not being called).

      If it doesn't work with your default theme, then it's likely a conflict with another plugin and you deactivate all your plugins except the one and see if it works. If the plugin then works, start reactivating your other plugins 1 by 1 until it stops working again. The last plugin you enabled is then the cause of the conflict.

      Debugging further than that isn't something I can really help with, especially for plugins and themes that I don't have any direct involvement with. The plugin authors or freelance contractors would be a better option.

      Like

  3. Thanks for the information by giving a brief description and let the customer know about Case-insensitive module URLs in Zend Framework routes.

    Like

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