Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/4084' into develop
Browse files Browse the repository at this point in the history
Forward port #4084
  • Loading branch information
weierophinney committed Mar 28, 2013
2 parents 1a8bb06 + 75c06ce commit cb00eec
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
42 changes: 40 additions & 2 deletions library/Zend/Navigation/Page/Mvc.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ class Mvc extends AbstractPage
*/
protected $controller;

/**
* URL query part to use when assembling URL
*
* @var array|string
*/
protected $query;

/**
* Params to use when assembling URL
*
Expand Down Expand Up @@ -205,14 +212,19 @@ public function getHref()
}

$options = array('name' => $name);
$url = $router->assemble($params, $options);

// Add the fragment identifier if it is set
$fragment = $this->getFragment();
if (null !== $fragment) {
$url .= '#' . $fragment;
$options['fragment'] = $fragment;
}

if (null !== ($query = $this->getQuery())) {
$options['query'] = $query;
}

$url = $router->assemble($params, $options);

return $this->hrefCache = $url;
}

Expand Down Expand Up @@ -284,6 +296,32 @@ public function getController()
return $this->controller;
}

/**
* Sets URL query part to use when assembling URL
*
* @see getHref()
* @param array|string|null $query URL query part
* @return self fluent interface, returns self
*/
public function setQuery($query)
{
$this->query = $query;
$this->hrefCache = null;
return $this;
}

/**
* Returns URL query part to use when assembling URL
*
* @see getHref()
*
* @return array|string|null URL query part (as an array or string) or null
*/
public function getQuery()
{
return $this->query;
}

/**
* Sets params to use when assembling URL
*
Expand Down
38 changes: 38 additions & 0 deletions tests/ZendTest/Navigation/Page/MvcTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,44 @@ public function testGetHrefWithFragmentIdentifier()
$this->assertEquals('/lolcat/myaction/1337#qux', $page->getHref());
}

public function testGetHrefPassesQueryPartToRouter()
{
$page = new Page\Mvc(array(
'label' => 'foo',
'query' => 'foo=bar&baz=qux',
'controller' => 'mycontroller',
'action' => 'myaction',
'route' => 'myroute',
'params' => array(
'page' => 1337
)
));

$route = new RegexRoute(
'(lolcat/(?<action>[^/]+)/(?<page>\d+))',
'/lolcat/%action%/%page%',
array(
'controller' => 'foobar',
'action' => 'bazbat',
'page' => 1,
)
);
$this->router->addRoute('myroute', $route);
$this->routeMatch->setMatchedRouteName('myroute');

$page->setRouteMatch($this->routeMatch);
$page->setRouter($this->router);

$this->assertEquals('/lolcat/myaction/1337?foo=bar&baz=qux', $page->getHref());

// Test with array notation
$page->setQuery(array(
'foo' => 'bar',
'baz' => 'qux',
));
$this->assertEquals('/lolcat/myaction/1337?foo=bar&baz=qux', $page->getHref());
}

public function testIsActiveReturnsTrueOnIdenticalControllerAction()
{
$page = new Page\Mvc(array(
Expand Down

0 comments on commit cb00eec

Please sign in to comment.