views:

244

answers:

3

I've never created a custom route before, but I finally have a need for one. My question is: How can I create a custom route, and where should I create it? I'm using Zend Framework 1.9.6.

+1  A: 

See the Router documentation for the how. And I would make your routes in the bootstrap. Either program the routes by hand or load a configuration file.

smack0007
+2  A: 

Here's where I define my custom routes:

// in bootstrap
Zend_Controller_Front::getInstance()->registerPlugin( new Prontiso_Controller_Plugin_Routes() );

...

<?php
// Prontiso/Controller/Plugin/Routes.php
class Prontiso_Controller_Plugin_Routes extends Zend_Controller_Plugin_Abstract
{
  public function routeStartup( Zend_Controller_Request_Abstract $request )
  {
    Prontiso_Routes::addAll();
  }
}

...

<?php
// Prontiso/Routes.php
class Prontiso_Routes extends Zend_Controller_Plugin_Abstract
{
  public static function addAll()
  {
    $router = Zend_Controller_Front::getInstance()->getRouter();

    /**
     * Define routes from generic to specific.
     */

    /**
     * Example: Simple controller/action route, guaranteed to eliminate any extra URI parameters.
     */
    $router->addRoute(
      'barebones', new Zend_Controller_Router_Route(':controller/:action')
    );
  }
}
Derek Illchuk
A: 

Here's how I did it. There may be a better way, but I think this is as simple as it gets:

# /application/Bootstrap.php
protected function _initSpecialRoutes()
{
    $router = Zend_Controller_Front::getInstance()->getRouter();
    $router->addRoute(
        'verify', new Zend_Controller_Router_Route('verify/:token', array(
            'controller' => 'account',
            'action' => 'verify'))
    );
    $router->addRoute(
        'arbitrary-route-name', new Zend_Controller_Router_Route('special/route/:variablename', array(
            'controller' => 'defaultcontrollername',
            'action' => 'defaultactionname'))
    );
}
Andrew