source code bean

23 Feb, 2009

Friendly URLs and the Zend Router

Posted by: Peter In: PHP| Zend Framework

Creating custom friendly URLs using the Zend framework is really simple. The default routing setup for Zend is : ‘:module/:controller/:action/*’, (* will match any var/value) which is fine for most setups. However on some pages having the var/value might not look very good, for example this url is not very readable:

/popular/index/type/images/page/1/sortOrder/alltime (controller/action/var/value/var/value/var/value)

We would prefer something like this:
/popular/images/1/alltime

Luckily Zend provides a very flexible router that we can configure as we want. To start with, we create a new config file called routes.ini, and adds these lines:

  1.  
  2. routes.popular.route = popular/:type/:page/:sortOrder
  3. routes.popular.defaults.controller = popular
  4. routes.popular.defaults.action = index
  5. routes.popular.defaults.type = images
  6. routes.popular.defaults.sortOrder = alltime
  7. routes.popular.defaults.page = 1
  8. routes.popular.reqs.type = \w+
  9. routes.popular.reqs.page = \d+
  10. routes.popular.reqs.sortOrder = \w+
  11.  

routes.popular.route tells us what to match. routes.popular.defaults.* sets default values for the variables (if none are given in the url). routes.popular.reqs.* sets requirements on the variables, for example that page must be a number.

The last thing we need to do is add a few lines to our bootstrap.php:

  1.  
  2. $config = new Zend_Config_Ini(APPLICATION_PATH . ‘/config/routes.ini’);
  3. $router = $frontController->getRouter();
  4. $router->addConfig($config,‘routes’);
  5.  



If you want to learn more about the Zend Framework:

6 Responses to "Friendly URLs and the Zend Router"

1 | Julian

June 18th, 2009 at 3:16 pm

Avatar

Good tks. It would be good to tell people where does $frontController comes from (Zend_Controller_From::getinstance()). Some people don’t know that.

2 | deerawan

November 13th, 2009 at 4:23 am

Avatar

I also use Zend Router to make my url more friendly

3 | Andrew Martin

December 30th, 2009 at 6:00 pm

Avatar

A very concise and configurable method, thanks!

4 | Vishal

February 3rd, 2010 at 2:37 pm

Avatar

I have made the changes as you have mentioned ..

1>I created a routes.ini file and put in my config folder
2>In my bootstrap file i have added the code as mentioned in your blog.

Then when i try to run my code for that particular link…
i am not getting the url friendly urls..

For your help…

—————–My routes.ini—————————–
routes.popular.route = popular/:type
routes.popular.defaults.controller = order
routes.popular.defaults.action = index
routes.popular.defaults.type = myorder
routes.popular.reqs.type = \w+
routes.popular.reqs.page = \d+
routes.popular.reqs.sortOrder = \w+
—————————————————–

——————-Bootstrap.php———————–

$config1 = new Zend_Config_Ini( APP_PATH .DIRECTORY_SEPARATOR . ‘/config/routes.ini’ );

$router1 = $front->getRouter();
$router1->addConfig($config1,’routes’);
————————————————————

Please help…

Do i need to make changes in any other place as well..like in the corresponding action…..

Thanks
Vishal

5 | Peter

February 14th, 2010 at 11:23 pm

Avatar

@Vishal What version of Zend Framework are you using? The default way of bootstrapping an zend application has changed some since I wrote the post.

6 | Petan

June 16th, 2010 at 12:45 pm

Avatar

I got in bootstrap this (insted of $frontController->getRouter()):

$config = new Zend_Config_Ini(APPLICATION_PATH . ‘/configs/routes.ini’, ‘routes’);
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addConfig($config, ‘routes’);

Comment Form

Categories

Adwords

Twitter Updates


    • Petan: I got in bootstrap this (insted of $frontController->getRouter()): $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini', 'rout
    • oanh tong ngoc: :) It's usefull but could U give everyone's an example with a project source code. Thanks
    • Peter: Hi Sohaib, It seems like the rewrite module isn't loaded by IIS. Have you uploaded the UrlRewriter dlls and made the changes to web.config on the ser

    About

    Welcome to source code bean! You will find information on tips and tricks on programming languages, server side stuff, and anything that causes troubles to web development.