views:

276

answers:

2

Is there anyway to create a vanity url "catch all" route whilst maintaining the default /module/controller/action/value routing structure?

Thanks!

+1  A: 

You could use the PreDispatch() hook on a front controller plugin. Like so:

In your bootstrap

<?php
...
$frontController = Zend_Controller_Front::getInstance();
// Set our Front Controller Plugin
$frontController->registerPlugin(new Mystuff_Frontplugin());

?>

Then inside Mystuff/Frontplugin.php

<?php

class Mystuff_Frontplugin extends Zend_Controller_Plugin_Abstract
{
    ....

    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        ....

        $controllerFile = $this->_GetRequestedControllerFile();

        if (!is_file($controllerFile)) {
            // Controller does not exist
            // re-route to another location
            $request->setModuleName('customHandler');
            $request->setControllerName('index'); 
            $request->setActionName('index');

        }
    }

    ....
}

Also preDispatch() is a handy location to handle application wide authentication.

Lance Rushing
Thanks Lance!Where do you set the plugin directory? I'm using Zend_Application with the application.ini file and I'm gettingFatal error: Class 'ControllerPlugins_Vanityurl' not found in C:\*****\library\Zend\Application\Resource\Frontcontroller.php on line 90 (I've got the ControllerPlugins directory in the library folder.Thanks!
arendn
Thanks Lance I've got it working now :-)
arendn
+2  A: 

It's better if you setup a custom route, for example in your bootstrap:

protected function _initRoutes() {
     $this->bootstrap('frontController');
     $front = $this->getResource('frontController');
     $router = $front->getRouter();

     $router->addRoute(
         'neat_url',
         new Zend_Controller_Router_Route(
              'profile/:username',
              array(
                   'controller' => 'profiles',
                   'action' => 'view_profile'
              )
         )
     );
}

This way you can still have the default route and have a custom route that will redirect everything under /profile/jhon.doe then under your controller you get the parameter using $this->_getParam('username');

Chris