jump to navigation

Zend Framework : Creating Custom Routes April 18, 2011

Posted by Tournas Dimitrios in Zend Framework v1.10.
trackback

The Zend Framework , during development ,  construct url’s that are mapped with the controllers and actions . For example a controller ” users” with an  action “list” will have a associated url “www.yourdomani.com/users/list ” . Of course the action could  behave dynamically , through accepting parameters , so the url would be like this : “www.yourdomani.com/user/list/parvar/parvalue ” . The action can retrieve the parameters as follows :

if ($this->_hasParam('parvar'))
{
  $this->_getParam('parvar');
}

To create a custom route you’ll use the framework’s Zend_Controller_Router_Route constructor to define the route with its associated controller, action, and parameters. In my article “Creating a simple Zend project ” , I made the configuration for a custom route inside the application.ini file . That’s ok … the Zend Framework can accept that , but in case your list of custom routes can be very long , this would be impractical . Fortunately for us there is an alternative way to define custom routes . A typical place to “set”  these routes is in your project’s Bootstrap.php file .

public function _initRoutes()
    {

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

        $route = new Zend_Controller_Router_Route_Static (
            'custroute1',
            array('controller' => 'users',
                  'action' => 'list')
        );

        $router->addRoute('custroute1', $route);

    }

As you add additional custom routes, you can just define them one after another within the _initRoutes() method. Just be sure to assign a unique name to each route when adding it to the routing chain via the addRoute method  , like this :

public function _initRoutes()
    {
        $frontController = Zend_Controller_Front::getInstance();
        $router = $frontController->getRouter();

        $route = new Zend_Controller_Router_Route_Static (
            'custroute1',
            array('controller' => 'books',
                  'action' => 'list')
        );
	$route2 = new Zend_Controller_Router_Route_Static (
            'custroute2',
            array('controller' => 'books',
                  'action' => 'list')
        );

        $router->addRoute('custroute1', $route)
	        ->addRoute('custroute2', $route2);

    }

Now accessing your resources via the browser can be done with “http://www.domain.com/custroute1 ” and  “http://www.domain.com/custroute2 “.  Of course the names of the custom routes must represent your websites schema .

I strongly suggest to read the manual .

Advertisements

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s