I'm building my first Zend Framework application and I want to find out the best way to fetch user parameters from the URL.
I have some controllers which have index, add, edit and delete action methods. The index action can take a page parameter and the edit and delete actions can take an id parameter.
Examples http://example.com/somecontroller/index/page/1 http://example.com/someController/edit/id/1 http://example.com/otherController/delete/id/1 Until now I fetched these parameters in the action methods as so:
class somecontroller extends Zend_Controller_Action { public function indexAction() { $page = $this->getRequest->getParam('page'); } } However, a colleague told me of a more elegant solution using Zend_Controller_Router_Rewrite as follows:
$router = Zend_Controller_Front::getInstance()->getRouter(); $route = new Zend_Controller_Router_Route( 'somecontroller/index/:page', array( 'controller' => 'somecontroller', 'action' => 'index' ), array( 'page' => '\d+' ) ); $router->addRoute($route); This would mean that for every controller I would need to add at least three routes:
- one for the "index" action with a :page parameter
- one for the "edit" action with an :id parameter
- one for the "delete" action with an :id parameter
See the code below as an example. These are the routes for only 3 basic action methods of one controller, imagine having 10 or more controllers... I can't imagine this to be the best solution. The only benefit that i see is that the parameter keys are named and can therefore be omitted from the URL (somecontroller/index/page/1 becomes somecontroller/index/1)
// Route for somecontroller::indexAction() $route = new Zend_Controller_Router_Route( 'somecontroller/index/:page', array( 'controller' => 'somecontroller', 'action' => 'index' ), array( 'page' => '\d+' ) ); $router->addRoute($route); // Route for somecontroller::editAction() $route = new Zend_Controller_Router_Route( 'somecontroller/edit/:id', array( 'controller' => 'somecontroller', 'action' => 'edit' ), array( 'id' => '\d+' ) $router->addRoute($route); // Route for somecontroller::deleteAction() $route = new Zend_Controller_Router_Route( 'somecontroller/delete/:id', array( 'controller' => 'somecontroller', 'action' => 'delete' ), array( 'id' => '\d+' ) $router->addRoute($route);