2
I have a problem using my url view helper. I have defined custom routes like so: 
; Index
routes.domain.type = 'Zend_Controller_Router_Route_Static'
routes.domain.route = '/'
routes.domain.defaults.controller = index
routes.domain.defaults.action = index

Everything works fine with the custom url's but I cannot assemble normal none. I tried to add a link using the following code from the view:

$this->url(array('controller' => 'search', 'action' => 'index');

The problem is that when I use this code in my index page of index controller, the returned url is the url of the current controller/action, and not the one I need.

JellyBelly
  • 2,451
  • 2
  • 21
  • 41
eroteev
  • 620
  • 1
  • 7
  • 17

2 Answers2

5

This is because the URL view helper picks to last active route. If you have multiple routes always define the route you are using:

$this->url(array('controller' => 'search', 'action' => 'index'), 'default');

The second parameter is the route to be used, an third optional parameter is if all params needs to be reset (true/false).

Kees Schepers
  • 2,248
  • 1
  • 20
  • 31
  • Duh... of course I should set the name of the route to 'default'. Thank you very much for the help Kees! – eroteev Nov 21 '11 at 06:58
1

For that you need to setup a reverse route map like explained here.

The most recommended way to generate a URL is by using your own custom URL view helper.

class My_View_Helper_FullUrl extends Zend_View_Helper_Abstract {

public function fullUrl($url) {
    $request = Zend_Controller_Front::getInstance()->getRequest();
    $url = $request->getScheme() . "://" . $request->getHttpHost(). "/" . $url;
    return $url;
  }
}

So, to generate a URL, you'll just call,

$this->fullUrl('search');

which will output,

www.example.com/search

Raj
  • 22,346
  • 14
  • 99
  • 142