Search code examples
phpzend-frameworkurlrouterzend-controller-router

Zend_Router omitting param-key


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


Solution

  • 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'))
    );