views:

28

answers:

1

Hey guys,

I've got a question considering Zend_Controller_Router. I'm using a a modular-structure in my application. The application is built upon Zend-Framework. The normal Routes are like this:

/modulename/actionname/

Since I always use an IndexController within my modules, it's not necessary to provide it in the url. Now I am able to append params like this:

/modulename/actionname/paramkey/paramvalue/paramkey/paramvalue

So this is normal in ZF, I guess. But in some cases I don't want to provide a paramkey within the url. For example I want a blog-title to be shown within the url. Of course this is intended for SEO:

/blog/show/id/6/this-is-the-blog-title

In this case, blog is the module, show is the action. id is a paramkey and 6 is the id of the blogpost I want to show. this-is-the-blog-title is of course the headline of the blogpost with the id 6. The problem is, that if I do use the assemble()-method of the router like this:

assemble(array('module' =>'blog',
               'action' => 'show', 
               'id' => $row['blog_id'],
               $row['blog_headline_de'] . '.html'));

the url results in:

blog/show/id/6/0/this-is-the-blog-title.html

As you can see a 0 is inserted as a key. But I want this 0 to be omitted. I tried this by using the blogtitle as key, like this:

assemble(array('module' =>'blog',
               'action' => 'show', 
               'id' => $row['blog_id'],
               $row['blog_headline_de'] . '.html' => ''));

This results in:

blog/show/id/6/this-is-the-blog-title.html/

Now the 0 is omitted, but I've got the slash at the end.

Do you have any solution to get an url without 0 as key and without an ending slash?

Regards, Alex

+1  A: 

You might want to use a custom route for this:

$router->addRoute(
    'blogentry',
    new Zend_Controller_Router_Route('blog/show/:id/:title',
                                     array('controller' => 'index', 'module' => 'blog'
                                           'action' => 'info'))
);

And call your assemble with the route as second parameter. See the Zend_Controller_Router_Route section of the documentation for more details (they even provide examples with assemble).

Or in a more general way:

$router->addRoute(
    'generalseo',
    new Zend_Controller_Router_Route(':module/:action/:id/:title',
                                     array('controller' => 'index'))
);
Fge
Thanks for your reply. The thing is, that I didn't want to create routes for every stuff. You see, I got the problem at other places to. for example in my portfolio module, etc. So I wanted to know, if there is a more general solution. But if there is not, I will have to do it like this. Thanks again.
faileN
You sure can make this rule more general (I'll add it to the original answer).
Fge
Yeah okay, of course I could do it this way. Problem is, that I can't guarantee that there will only be a ":id" param, but also some others in front of the title. At least I think that this can occur, when I extend my application. But thanks again. I will use it this way by now.
faileN
"*" mostly will help you then. Have a look at the last paragraph of Zend_Controller_Router_Route for details.
Fge