Zend framework custom router

01Jan12

This blog is moved to webhkp.com

There are more than one way to implement custom router in zend framework. You can do it in bootstrap, you can also do it using an ini file. I like to do it in ini file. This way it becomes manageable and you can add as much route you want one by one.

Let me tell you the way i follow.

At first add a new function to you Bootstrap:

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 tells the system to get the routing information from “routes.ini” file from application/configs.

So you need a routes.ini file in application/configs. Create a new file named routes.ini there. and you are ready to go………..

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

put the line bellow in routes.ini file:
[production]
routes.home.route = /home
routes.home.defaults.controller = index
routes.home.defaults.action = index

[production] represents your development environment. and in other lines the route name, controller and action is defined

Now say you have a section where you show users their account information. its in module “user”, the controller is “indexController” and the action is “infoAction”

So by default the url should be like this:

http://testzfroute.com/user/index/info

But you want to make the url like http://testzfroute.com/profile

then you have to add the line as bellow in routes.ini file:

routes.profile.route = /profile
routes.profile.defaults.module = user
routes.profile.defaults.controller = index
routes.profile.defaults.action = info

That’s it………

If you have any question regarding this, let me know…..

good luck and happy routing…..:)

About these ads


One Response to “Zend framework custom router”

  1. 1 Faisal

    You Save me dude … :)


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 )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: