views:

2777

answers:

3

Hi! How can I shorten the definition of my custom routes in Zend Framework? I currently have this as definition:

$route = new Zend_Controller_Router_Route(
 ":module/:id",
 array(
  "controller" => "index",
  "action" => "index" 
 ),
 array("id" => "\d+")
);
self::$frontController->getRouter()->addRoute('shortcutOne', $route);

$route = new Zend_Controller_Router_Route(
 ":module/:controller/:id",
 array("action" => "index"),
 array("id" => "\d+")
);
self::$frontController->getRouter()->addRoute('shortcutTwo', $route);

$route = new Zend_Controller_Router_Route(
 ":module/:controller/:action/:id",
 null,
 array("id" => "\d+")
);
self::$frontController->getRouter()->addRoute('shortcutThree', $route);

Is there a way to better combine these rules? And what are your best practices in where to place these? I currently have them in my bootstrap class right after the Front Controller initialization.

+3  A: 

When it comes to setting up routes like this, I use a config file. As a preference, I use XML to store my config data, however these could just as easily be stored in another supported format. I then add the routes from the config, to the router in my bootstrap.

Config:

<config>
    <routes>
        <shortcutone  type="Zend_Controller_Router_Route">
            <route>:module/:id</route>
            <defaults>
                <controller>index</controller>
                <action>index</action>
            </defaults>
            <reqs id="\d+">
        </shortcutone>
        <shortcuttwo  type="Zend_Controller_Router_Route">
            <route>:module/:controller/:id</route>
            <defaults>
                <controller>index</controller>
            </defaults>
            <reqs id="\d+">
        </shortcuttwo>
        <shortcutthree  type="Zend_Controller_Router_Route">
            <route>:module/:controller/:action/:id</route>
            <defaults>
                <controller>index</controller>
                <action>index</action>
            </defaults>
            <reqs id="\d+">
        </shortcutthree>
    </routes>
</config>

Bootstrap

$config = new Zend_Config_Xml('config.xml');
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addConfig($config, 'routes');

Obviously, there are other options and I'd encourage you to read the documentation on this, however, this fits for your example.

Kieran Hall
Thanks. I will use the config then to keep my Bootstrap slim :)
Sebastian Hoitz
+1  A: 

My routes.ini file started to get really large, so I decided to use Zend Caching to cache the routes after they had been parsed. I used Xcache for the backend caching solution. Here's the code, which should be put in the Bootstrap.php file:

protected function _initRoutes()
{
  $backendType = 'Xcache';
  $backendOptions = array();

  // Instantiate a caching object for caching the routes
  $cache = Zend_Cache::factory('File', $backendType, 
    array(
      'automatic_serialization' => true, 
      'master_files'=>array(APPLICATION_PATH . '/configs/routes.ini')
    ), 
    $backendOptions
  );

  $frontController = Zend_Controller_Front::getInstance();        

  if(! $router = $cache->load('router')) {

    // Load up .ini file and put the results in the cache
    $routes = new Zend_Config_Ini (APPLICATION_PATH . '/configs/routes.ini', 'production');            
    $router = $frontController->getRouter();
    $router->addConfig( $routes, 'routes' );

    $cache->save($router, 'router');
  }     
  else {        
    // Use cached version
    $frontController->setRouter($router);
  }

}
Bret Truchan
+1  A: 

I prefer to use *.ini files over XMLs especially when using Zend since it's more Zend-like and much more light-weight and compact. Here's an almost similar configuration using Zend_Config_Ini().

application.ini

[routes]
routes.shortcutone.route=:module/:id
routes.shortcutone.defaults.controller=index
routes.shortcutone.defaults.action=index
routes.shortcutone.reqs=\d+

bootstrap.php

$config = new Zend_Config_Ini('application.ini', 'routes');
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addConfig($config, 'routes');

Take note that the [routes] section in the application.ini file can be renamed. And when it is renamed, the second parameter of Zend_Config_Ini() should reflect the new section title.

Randell