Custom Routes and Retrieving Keyless Parameters in Zend Framework 1.11

You can specify what specific path fields should be mapped to. In this example will recognize a path without the parameter name and just the value. So you can have a path like /action/value/value instead of /action/key/value/key/value. Having a keyless path is a cleaner compact url.

You will need to create a custom route in /appliation/Bootstrap.php. The following will route any path that looks like /portfolio/edit/xx to the portfolio edit action passing the record id in the xx position. You will be able to use this kind of path /portfolio/edit/1001 instead of /portfolio/edit/id/1001.


 public function _initRoutes()
    {
        $frontController = Zend_Controller_Front::getInstance();

        $router = $frontController->getRouter();

        $router->addRoute(
           'portfolioedit',
           new Zend_Controller_Router_Route(
                '/portfolio/edit/:id',
                array('controller' => 'portfolio', 'action' => 'edit')
           )
        );
    }

You can now retrieve the url value by using getParam() looking for the ‘id’ field In the portfolio controller editAction() method.


$id = $this->getRequest()->getParam('id');

Leave a comment

0 Comments.