Question

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

Thanks!

Was it helpful?

Solution

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.

OTHER TIPS

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');

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top