--- In php-list@yahoogroups.com, "Junior Grossi" <[EMAIL PROTECTED]> wrote:
>
> Hi all,
> 
> I want to change my url site...
> 
> I have http://mysite.com/posts/show?id=1234
> I want to change to http://mysite.com/posts/some-title-name
> 
> I have looked the Zend site and I think that I have to do this:
> 
> $route = new Zend_Controller_Router_Route(
>     'posts/:title',
>     array(
>         'controller' => 'posts',
>         'action'     => 'show'
>     )
> );
> $router->addRoute('postShow', $route);
> 
> I dont know how can i send the url and i dont know where i send and
> get the post_id in the controller...
> 
> Am i doing something wrong?

Junior,

With the example above, there is no post id.  If you try the url as
http://mysite.com/posts/some-title-name/id/10

Then inside your controller action you could do $id =
$this->_getParam('id').  With the way you have it, you'd need to look
up the post by the title.  That could be tough since the title has
been mangled to work in a URL.  Also, it means that your post titles
would need to be unique.  Instead, I'd probably include the post id in
the url rewrite rule like so:

$route = new Zend_Controller_Router_Route(
    'posts/:id/:title',
    array ('controller' => 'posts',
           'action' => 'show'
           ),
    array ('id' => '\d+'));
$router->addRoute('postShow', $route);

This means your url would be:
http://mysite.com/posts/10/some-title-name

In this case, too, when you do $this->_getParam('id'), you'll get the
id (10 in this case).  $this->_getParam('title') would give you
"some-title-name".  If you go with this way, the title part is not
really important for finding the post, although I understand it can
help with SEO.

The last parameter in the route setup ( array('id => '\d+') ) is a
regular expression to let the router know what are valid values for
that parameter posistion.  In this case it's saying 1 or more digits.
 If you were to try something like

http://mysite.com/posts/1b2/some-title-name, the dispatcher would try
to route to the 1b2 action in your posts controller.  You could also
deal with non-digit characters inside your controller rather than have
an error thrown.

Hope this helps,
David


Reply via email to