eroteev
eroteev

Reputation: 610

Zend: The url assembled by the URL view helper is incorrect

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.

Upvotes: 2

Views: 2130

Answers (2)

Kees Schepers
Kees Schepers

Reputation: 2258

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).

Upvotes: 5

Raj
Raj

Reputation: 22956

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

Upvotes: 1

Related Questions