Here's an example:

1. Define your regex route in Bootstrap

Put this code in application/Bootstrap.php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

  // other _initXXX

  protected function _initRoutes()
  {

    $front = Zend_Controller_Front::getInstance();    
    
    $router = $front->getRouter();  

    // Add a test regex route
    $router->addRoute('article',
                    new Zend_Controller_Router_Route_Regex
                    (
                     "article(?:/(\d+))?",
                     array(
                           'controller'=>'article',
                           'action'=>'view',
                           1=>123,  //set a default value, when url is "http://host/article/"
                           ),
                     array(
                           1=>'id' // use 'id' instead of 1
                           ),
                     "article/%d"  // reverse to url , ie $view->url(array('id'=>233),'article',true)
                     )
                    );



  }
}
    

2. Create controller and action

$ zf.sh create controller article 1
$ zf.sh create action view Article 1

That's all.

Reference: Programmer's Reference Guide : Zend_Controller : Zend_Controller_Router_Route_Regex

Date: 2012-01-03 04:01:20 and last modified: 2012-01-14 22:53:07

Relate tags:

Hot tags: