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.
views:
244answers:
3
+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
2009-12-08 14:14:03
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
2009-12-15 05:09:03