Zend Framework Registry Step By Step Tutorial - Part 2: In this part we talk how to register array value and how to read them? Every value that we store at registry can we read as array. Ok, follow this steps to understand.
Open again your "index.php" within web_root. Update become like this:
<?php
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', true);
date_default_timezone_set('Europe/London');
$rootDir = dirname(dirname(__FILE__));
set_include_path($rootDir . '/library' . PATH_SEPARATOR . get_include_path());
require_once 'Zend/Controller/Front.php';
require_once 'Zend/Registry.php';
Zend_Registry::set('title',"My First Application");
$arrName = array('Ilmia Fatin','Aqila Farzana', 'Imanda Fahrizal');
Zend_Registry::set('credits',$arrName);
Zend_Controller_Front::run('../application/controllers');
?>
We register a array parameter named "credits" at line 15-16. How to read it?
Ok, we test by update again "UserController.php". Update at indexAction become:
public function indexAction()
{
$registry = Zend_Registry::getInstance();
$title = $registry['title'];
$credits = $registry['credits'];
$strCredit = implode(", ",$credits);
$this->view->assign('name', 'Wiwit');
$this->view->assign('title', $title);
$this->view->assign('credits', $strCredit);
}
This is another way to read registry. It can we use to read several values from registry.
Ok, before test it. Please update view at index.phtml within application/views/scripts/user. Update with following code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><? echo $this->escape($this->title); ?></title>
</head>
<body>
<h1><?=$this->escape($this->title);?>, <?=$this->escape($this->name);?></h1>
credits: <?=$this->escape($this->credits);?>
</body>
</html>
Test by point your browser to http://localhost/test/zend/helloworld/web_root/user/.