ZF-7794: Navigation with Scheme classification

Description

I'm unsure if there is already a method of achieving this but from what I could see there wasn't. I ran into a position where I needed to ensure two things:

a. make sure the link to checkout was always an https link b. ensure other links on the site were http protocal so that the user didn't get trapped in the https

Checking Zend_Navigation I didn't see a way to acheive this, this is the alterations I made to make it work:

Zend/Navigation/Page/Mvc.php


/**
     * Scheme to use when assembling URL
     *
     * @see getHref()
     * @var string
     */
    protected $_scheme;

/**
     * View helper for assembling URLs with schemes
     *
     * @see getHref()
     * @var Zend_View_Helper_ServerUrl
     */
    protected static $_schemeHelper = null;

$url = self::$_urlHelper->url($params,
                                      $this->getRoute(),
                                      $this->getResetParams());

        if ($this->getScheme() != null)
        {
            if (null === self::$_schemeHelper)
            {
               self::$_schemeHelper = new Zend_View_Helper_ServerUrl();
            }
            
            $url = self::$_schemeHelper->setScheme($this->getScheme())->serverUrl($url);
        }

        return $this->_hrefCache = $url;

/**
     * Sets scheme to use when assembling URL
     *
     * @see getHref()
     *
     * @param  string|null $module        scheme
     * @return Zend_Navigation_Page_Mvc   fluent interface, returns self
     */
    public function setScheme($scheme)
    {
        $this->_scheme = $scheme;
        return $this;
    }

    /**
     * Returns scheme to use when assembling URL
     *
     * @see getHref()
     *
     * @return string|null  scheme or null
     */
    public function getScheme()
    {
        return $this->_scheme;
    }

If theres already a built in method of acheiving this it would be great to hear how.

Cheers,

Jay

Comments

You could call setScheme('http') on the ServerUrl helper and manually specify https for the page which needs it.

Patch and unit tests added.

Fixed in trunk (1.12.0): r24964