Tag Archives: zend cuctom route

How to create/setup custom routing in ZendFramework

Routing is the process of taking a URI endpoint (that part of the URI which comes after the base URL) and decomposing it into parameters to determine which module, controller, and action of that controller should receive the request.
In simple way, we can say that this is same as URL Rewriting in PHP we do.

Ex1 : Routing without variables

Now, say for example your domain is http://mydomain.com
And you want the http://mydomain.com/register/user to be http://mydomain.com/user-registration

Put the code below in Bootstrap.php file as

1
2
3
4
5
6
7
<?php
protected function _initRoute()
{
    $router = Zend_Controller_Front::getInstance()->getRouter(); //create object of a router
    $router->addRoute('registration',new Zend_Controller_Router_Route('user-registration',array('controller'=>'register','action'=>'user'))); //registration can be any text as you want
}
?>

Ex2 : Routing with variables

Now, say for example http://mydomain.com/product/10

Put the code below in Bootstrap.php file as

1
2
3
4
5
6
7
<?php
protected function _initRoute()
{
    $router = Zend_Controller_Front::getInstance()->getRouter(); //create object of a router
    $router->addRoute('myproduct',new Zend_Controller_Router_Route('product/:id',array('controller'=>'product','action'=>'index'))); //myproduct can be any text as you want 
}
?>

Hope, you will enjoy!