Creating custom routes in your zend framework application

One of my zend framework project need to define custom routs . I have google a lot then found a solution.

Here I share my solution.

You can define custom routes in zend framework in many ways. I create a ini file to do this.

First add those line to your Bootstrap file.

protected function _initRoutes() {
$this->bootstrap('frontcontroller');
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$myRoutes = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini', 'production');
$router->addConfig($myRoutes, 'routes');
}

This will tell the system routing information from “routes.ini” file from application/configs.

Then you need a routes.ini file in application/configs. Now create a new file named routes.ini there.

Now say for example your domain is http://yourdomain.com and you want the http://yourdomain.com/index to be http://yourdomain.com/live

put the line bellow in routes.ini file:

[production]
routes.live.route = /live
routes.live.defaults.controller = index
routes.live.defaults.action = index
[production] represents your development environment.
Now if you create a module and need your custom route you can write this way. Say if you have a module calleduser , controller index and action index. Then your route should be like this.
http://yourdomain.com/user/index
But you want to make the url like http://yourdomain.com/info
routes.info.route = /info
routes.info.defaults.module = user
routes.info.defaults.controller = index
routes.info.defaults.action = index

Enjoy and Happy Coding 🙂

Advertisements

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