ZF View Helper for switching between http and https

I created a view helper for Zend Framework for switching between a normal (http) and secure (https), as sugested by Naneau.

<?php
/**
* Helper class to switch between normal and secure protocol
*
*/
class Zend_View_Helper_HttpSwitch {

/**
* Property of our view instance
*/
protected $_view;

/**
* Get our view property
*/
public function setView(Zend_View_Interface $view)
{
$this->_view = $view;
}

/**
* Switches between http and https protocol
*
* @param array $route
* @param boolean $secure
* @return string
*/
public function httpSwitch(array $route, $secure = false)
{
// Hostname now set here, or in configuration file
// or set in the front controller.
// Zend_Controller_Front::getInstance()->getParam('hostname');
$hostname = "www.example.com";

// Check if we want to go secure or not
if ($secure === true) {
$protocol = "https";
} else {
$protocol = "http";
}
return sprintf("%s://%s%s", $protocol, $hostname, $this->_view->url($route));
}
}



Blogged with Flock

Comments

Post a Comment

Popular Posts