views:

48

answers:

3

Hi guys, I'm using the zend framework for my project - I've tried to set up using clean links - however I've noticed that my links seem to be getting really really long and wonder if at this point there would be a way to incorporate a structure where I could user smaller links.

Heres an example my website has a directory controller, which has a person action which takes an id parameter - so then a link to see a person of a certin id looks like:

www.mywebsite.com/directory/person/id/1809

It seems ok but I'm working now to make links more readable and search engine friendly - I would like to have something like - assuming the user ID 1809 is named John Smith:

www.mywebsite.com/directory/person/John-Smith-1809

Something really basic like the following would be even better:

www.mywebsite.com/John-Smith-1809

Although I don't know how I could manage that without mentioning a controller or action...any ideas guys would really help...

A: 

You can use the apache module mod_rewrite to rewrite your URL's

http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

<IfModule mod_rewrite.c>

  RewriteEngine On

  RewriteCond ###blah blah blah###
  RewriteRule ###blah blah blah###

</IfModule>
Jon Winstanley
I got that part - I'm asking for a technique to really shorten my urls here...
Ali
Ok cool. You can use mod_reqrite to shorten URL's by mapping short URLs' to your current versions.
Jon Winstanley
+3  A: 

Have a look at the ZF Manual on how to use Routes.

Basically, you need something like this to make it work:

$route = new Zend_Controller_Router_Route_Regex(
    '(.+)-(\d+)',
    array(
        'controller' => 'directory',
        'action'     => 'person'
    ),
    array(
        1 => 'name',
        2 => 'id'
    ),
    '%s-%d'
);
$router->addRoute('directoryPerson', $route);

Note: don't expect this to run out of the box. You will likely have to modify it. Should get you into the right direction though.

Gordon
+2  A: 

Like a previous answer said you could use a Regex route, but for starters it would probably be easier to use a "regular" route that looks like:

http://yoursite.com/1234/John-Smith/

In your bootstrap you'd do:

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

$router->addRoute('user', new Zend_Controller_Router_Route(
    ':user-id/:user-name/',
    array(
        'module' => 'user',
        'controller' => 'user',
        'action' => 'read'
    )
));

This adds a route called "user" to the router, that has the form /user-id/user-name, so /1234/John-Smith/. It will look for a usercontroller in a user module, and go to the read action. You can of course change that to another module/controller/action if you wish.

You'd then have that User_UserController in the User module, that has a readAction(); In that action you could get the id with:

$userId = $this->_getParam('user-id');

Similary, should you need it, you could retrieve the name.

$userName = $this->_getParam('user-name');

In a view script you could generate an url to that route with:

<?php echo $this->url(array('user-id' => 1234, 'user-name' => 'John Smith'), 'user', true); ?>

Note the 'user' parameter, which tells the router to use the "user" route we defined earlier.

naneau