Posted by: Asit | March 24, 2009

Zend Framework:: Helper method for creating breadcrumbs

Here I am going to show how one can create breadcrumbs in the web application based on Zend Framework. This can be achieved by creating a helper method. Before creating helper method, we need to do the following exercise:

  • Create Zend view object,
  • Add view script path,
  • Add the default helper path to the view object,

Now lets start step by step:

Creating Zend view object:

$view = Zend_View();

Adding view script path:

$viewPath = “path/to/viewsDirectory”;
$view->addScriptPath($viewPath);

Adding the default helper path to the vew object:

$helperPath = “path/to/helperDirectory”;      Zend_Controller_Action_HelperBroker::getStaticHelper(‘viewRenderer’)->setView($view);
$view->addHelperPath($helperPath, ‘Helper_’);

Now creating the class for the breadcrumbs:

class Helper_BreadCrumbs {

public function breadCrumb(){
$cFront = Zend_Controller_Front::getInstance();
$module = $cFront->getRequest()->getModuleName();
$module = strtolower($module);

$controller = $cFront->getRequest()->getControllerName();
$controller = strtolower($controller);

$action = $cFront->getRequest()->getActionName();
$action = strtolower($action);

if($module == “default” && $controller == “index” && $action == “index”){
return;
}
//Home link
$home = ‘<a href=”/”>Home</a>’;

//start breadcrumbs
$bCrumbs = $home . “::”;
if($action == “index”){
$bCrumbs .= $controller;
}
else{
$bCrumbs .= “<a href=’/$controller’>$controller</a>::
<a href=’/$controller/$action’>$action</a>”;
}

return $bCrumbs;
}
}

Hope this article will help the web application users to indicate “where are you”.


Leave a response

Your response:

Categories