views:

84

answers:

1
protected function _initRoutes()
{
    $this->router = $this->frontController->getRouter();
    $route = new Zend_Controller_Router_Route(
        ':username',
        array(
            'module'     => 'default',
            'controller' => 'view',
            'action'     => 'profile',
            'username'   => ':username'
        )
    );
    $this->router->addRoute('profile', $route);
}

What it's supposed to do is match this:

http://www.mydomain.com/something

To:

http://www.mydomain.com/view/profile/username/something

Which works. The trouble is when I go to:

http://www.mydomain.com

I get a a long database error which is there basically because it is matched to (and it shouldn't be):

http://www.mydomain.com/view/profile

But without a username, which is required.

The route is defined in my bootstrap file. What should I do to make it work right?

EDIT:

It seems the problem is with the url helper in my views. What is wrong with these URLs?

<?php

echo $this->url(array('module' => 'default',
                      'controller' => 'view',
                      'action' => 'profile',
                      'id' => $this->escape($m->id)),
                null,
                true);

                ?>

Or:

<?php

echo $this->url(array('module' => 'default',
                      'controller' => 'my-account',
                      'action' => 'write-message'),
                null,
                true);

            ?>
+2  A: 

The line

'username'   => ':username'

This means you're setting the paramater username default value to the string ':username', if you leave it out of the route def, if there is no username it will just ignore this route and move on.

protected function _initRoutes()
{
    $this->router = $this->frontController->getRouter();
    $route = new Zend_Controller_Router_Route(
        ':username',
        array(
            'module'     => 'default',
            'controller' => 'view',
            'action'     => 'profile',
        )
    );
    $this->router->addRoute('profile', $route);
}
linead
Yes that line is totally useless
andho
It's not only useless, but the cause of the problem. By having a default value for the username it will always have a value, meaning that this route will always match.
linead
When I delete that line the www.mydomain.com works fine again but when I go to www.mydomain.com/username I get an error: "username is not specified"
Richard Knop
What version of ZF are you using, it seems to work fine for me?
linead
I'm using the latest ZF version, but I'm using modular structure if it matters.
Richard Knop
Hi. I have a feeling the problem is with the way I'm using the view helper in my views. Could you please check the example urls I added to my question?
Richard Knop