Passing parameters with Route in Zend Framework

When first approaching the Zend Framework, it almost appears that in order to get URLs like /controllername/page1 or /controllername/page2 (equivalent to /controllername/?varname=page1 w/o ZF) with their preconceived directory structure, you have to create a new action for each new page in order to serve the page.  This would ultimately mean a new function for every single page within that controller; however, with Zend_Controller_Front and Zend_Controller_Router_Route, we can avoid this and pass page1, page2, and any other string as variables.

To illustrate this concept, we will be using the recommended Zend Framework setup, which can be created with Zend_Tool by executing the zf create project . command (See for the Getting Started tutorial if you are unfamiliar with ZF and the suggested directory structure).

  1. Open your Zend Framework-based application in your favorite IDE.

    Zend Framework in Eclipse PDT
    Zend Framework in Eclipse PDT
  2. Create a new controller. For this example, we will be using a controller named library.  This can be done manually or via the Zend_Tool CLI.  I prefer the CLI because it is extremely easy to use.
    1. Using the CLI: Access the command prompt.  To access the command prompt in Windows, go to Start, click Run, type cmd, and press enter.  Make sure that you are in your project’s root directory, then type zf create controller library.
    2. Manually: Create the following files…
      • /application/controllers/LibraryController.php
      • /application/views/scripts/library/index.phtml
      • /application/views/scripts/library/page.phtml

    Command Prompt: zf create controller library
    Command Prompt: zf create controller library
  3. Open up /public/index.php and find $application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . ‘/configs/application.ini’) which typically starts on line 21.

    Zend_Application in index.php
    Zend_Application in index.php
  4. Type the following information just underneath the line we located in step 4.
    /** Routing Information */
    $frontController = Zend_Controller_Front::getInstance();
    $router = $frontController->getRouter();
     * Add /library/page-name-here route.
     * :librarypage can be changed to alter the name of the variable being
     * passed.  This changes how the variable is referenced from the
     * $_request variable in your controller, and not much else.
     * The $router->addRoute() method can be used to add other methods as well.
        new Zend_Controller_Router_Route('library/:librarypage', array(
            'controller' => 'library',
            'action' => 'page'
  5. Open up /application/controllers/LibraryController.php and make sure it looks like this.
    class LibraryController extends Zend_Controller_Action {
        public function init() {
            /* Initialize action controller here */
        public function indexAction() {
            // action body
        public function pageAction() {
        	// :librarypage reference
        	$this->view->page_id = $this->_request->getUserParam('librarypage');
  6. Open /application/views/scripts/library/page.phtml, and add the following line:
    <p><strong>Parameter [librarypage]: </strong><?php echo $this->page_id; ?></p>

If you open http://your-project-address/library/my-page you should see the following in your browser:

Screenshot of the end result
Screenshot of the end result

One thought on “Passing parameters with Route in Zend Framework”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>