zend-frameworkzend-routezend-router

Zend Created route and now all links use this url


I created a route to view users profiles:

$router = Zend_Controller_Front::getInstance()->getRouter();
    $route  = new Zend_Controller_Router_Route(
            'profile/:username',
            array(
                'username'      => 'username',
                'module'        => 'core',
                'controller'    => 'profile',
                'action'        => 'view'   
            ) 
    );
    $router->addRoute('profile',$route);

When I go there, all of the urls within the page all now say http://127.0.0.1/project/public/profile.

How do I fix this?


Solution

  • If using the Url view helper or Zend_Navigation, you need to specify the route to use if you don't want it to use the current one. For example...

    Url view helper

    Use the "default" route

    <?php echo $this->url(array(
        'action'     => 'index',
        'controller' => 'index'
    ), 'default', true) ?>
    

    Navigation config

    resources.navigation.pages.home.label      = "Home"
    resources.navigation.pages.home.action     = "index"
    resources.navigation.pages.home.controller = "index"
    resources.navigation.pages.home.module     = "default"
    
    ; Don't forget to set the route
    resources.navigation.pages.home.route      = "default"
    

    Addendum

    The easiest way to configure routes is via the Router resource. Try this in your configuration instead of the code you have

    resources.router.routes.profile.route               = "profile/:username"
    resources.router.routes.profile.defaults.module     = "core"
    resources.router.routes.profile.defaults.controller = "profile"
    resources.router.routes.profile.defaults.action     = "view"