jump to navigation

Zend Framework: Loading images with a View_Helper May 13, 2011

Posted by Tournas Dimitrios in Zend Framework v1.10.
trackback

In our view scripts, often it is necessary to repeat certain complex functions over and over : e.g., formatting a date, generating form elements, or displaying action links . You can use helper classes to perform these behaviors for you . Although Zend provides us with over two dozen build-in View helpers ranging from meta data  to navigation helpers , sometimes we have to build  a custom View helper ourself , due to overcome repetitive functionality .  This article will demonstrate a practical example how to create an image loader View_Helper Class and use it all over the Zend application .

The steps to be followed are :

  • Creating the image loader Class : First create the directory structure to house our Class
    Project_Root/library/YourName/View/Helper/
    and the directory for the images :
    Project_Root/public/images/

  • Registering the image loader in the Bootstrap.php file
  • Accessing the helper Class all over the application

Creating the image loader Class :

<?php
/**
  * View Helper:
 **/
class View_Helper_Img extends Zend_View_Helper_Abstract
{
	public function img($src, $width, $height, $alt)
    {	
       if (empty($alt))
	   {            
    throw new Zend_View_Exception('Alt attribute should be filled!!');
        }
    $basepath = $this->view->serverUrl() .'/images/' ;
	$img = '<img src="' . $basepath . $src . '" height="' . $height . '" alt="' . $alt . '" width="' . $width . '">';
		return $img;

    }
    public function echosth($msg)
	{
	$rtnmsg = '<br><b>'.$msg .'</b>';
        return $rtnmsg ;
    }
}

Registering the image loader in the Bootstrap.php file :

<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
	{
 protected function _initView()
		{
    $view = new Zend_View();
    $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
        'ViewRenderer'
    );
    $viewRenderer->setView($view); 
    $view->addHelperPath('../library/Tdimitrios/View/Helper/', 'View_Helper'
	);	
    //Zend_Registry::set('view', $view);
    return $view;
		}
 
	}

Accessing the helper Class all over the application :

<br /><br /><center>View script for controller <b>Viewhelper</b> and script/action name <b>index</b></center>
<?php
echo $this->img('logo.jpeg', 100, 100 ,'this is an image');
echo $this->getHelper('Img')->img('logo.jpeg', 100,100,'this is an image');
echo $this->getHelper('Img')->echosth("some text");
Advertisements

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s