Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Created July 21, 2011 15:03
Show Gist options
  • Save wilmoore/1097385 to your computer and use it in GitHub Desktop.
Save wilmoore/1097385 to your computer and use it in GitHub Desktop.
Custom Zend Framework Url Helper (honors existing query string -- works well for pagination links, etc.)
<?php
/**
* @copyright ...
* @author Wil Moore III <wil.moore@wilmoore.com>
*/
use Zend_View_Helper_Url as Url;
/**
* View Helper to attach a query-string to URL
* Inspired by: (Hector Virgen)
* http://zend-framework-community.634137.n4.nabble.com/Generating-URL-s-View-Helper-Action-Helper-tp3002936p3004194.html
* Also see : http://framework.zend.com/issues/browse/ZF-11042?focusedCommentId=46896#comment-46896
*
* @copyright ...
* @author Wil Moore III <wil.moore@wilmoore.com>
*/
class m3_core_view_helper_Url extends Url {
/**
* Proxies to Zend_View_Helper_Url and honors any existing query string
* NOTE: If $reset evaluates to true, the query string will not be added
*
* You will need to add to your application.ini:
* resources.view.helperPath.m3_core_view_helper = SOURCE_PATH "/library/m3/core/view/helper"
* or for ZF2
* resources.view.helperPath.m3\core\view\helper\ = SOURCE_PATH "/library/ets/core/view/helper"
*
* @access public
*
* @param array $urlOptions
* Options passed to the assemble method of the Route object.
* @param mixed $name
* The name of a Route to use. If null it will use the current Route
* @param bool $reset
* Whether or not to reset the route defaults with those provided
*
* @return string Url for the link href attribute.
*/
public function url(array $urlOptions = array(), $name = null, $reset = false, $encode = true, $includeQueryString = true) {
// get the same URL that the intrinic URL helper would get
$url = parent::url($urlOptions, $name, $reset, $encode);
// get query-string
$query = parse_url(urlencode($this->getRequestUri()), \PHP_URL_QUERY);
// if there is no query string or
return empty($query) || $reset
? $url
: sprintf('%s?%s', $url, $query);
}
/**
* Retrieves the requested URI
*
* @access protected
* @return string
*/
protected function getRequestUri() {
return \Zend_Controller_Front::getInstance()->getRequest()->getRequestUri();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment