Archive

Archive for the ‘Zend Framework’ Category

Zend Framework:: Helper method for creating breadcrumbs

March 24, 2009 2 comments

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”.

Categories: Zend Framework

Zend Framework:: Caching the database query results

March 20, 2009 1 comment

Caching is basically used to improve the performance of the application. Your application may consists of multiple expensive queries ruining the application performance.  The Zend_Cache component of Zend Framework provides a good feature to improve the page performance by caching the database query results. Here is the sample code :

// Creating the cache options
$frontend = array(
‘lifetime’ => 86400, // cache lifetime of 24 hours (time is in seconds)
‘automatic_serialization’ => true  //default is false
);

$cachedir = /path/to/cacheDirectory;
if(!is_dir($cachedir)){
mkdir($cachedir,0755);
}

$backend = array(‘cache_dir’ => $cachedir);

// Getting a Zend_Cache_Core object
$zend_cache = Zend_Cache::factory(‘Core’, ‘File’, $frontend, $backend);

$result = run_query($zend_cache);
print_r($result);

function run_query($cacheObj){
$q = “Database query”;
if(!$result = $cacheObj->cache->load(“myresult”)){
$db = Zend_Db::factory(DB_connection_parameters);
$result = $db->fetchAll($q);
$cacheObj->save($result, “myresult”);
}
else {
echo “This result is from cache.”;
}
return $result;
}

Caution: You should give the unique name in the “load(‘name’) and save(‘name’)” functions.
Otherwise you will get the same result on the pages where the query is intended to produce
different results.

Categories: Zend Framework