10a Zend Framework Registry string tömb objektum tárolása

Default book

A Zend_Registry az az osztály, amely a php globális változói helyett használható.

Használata során a Zend Registrybe betölthetünk adatokat:

  • egy stringet
  • egy tömböt
  • Objektumot

Ezeket az adatokat betölthetjük és az oldal futása során bármikor elérhetjük. A program lefutásával az adatokat - ha kell célszerű lementeni egy sessionbe, illetve a szükséges adatokat elmenteni egy adatbázisba profilként.

Módosítsuk az index.php oldalt az alábbiak szerint:

<?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);
 
$params = array('host'     =>'localhost',
                'username' =>'root',
                'password' =>'admin',
                'dbname'   =>'zend'
               );
$DB = new Zend_Db_Adapter_Pdo_Mysql($params);
     
$DB->setFetchMode(Zend_Db::FETCH_OBJ);
Zend_Registry::set('DB',$DB);
Zend_Controller_Front::run('../application/controllers');

?>

A kiemelt sorok segítségével definiálhatunk egy regisztrációs adatbázist, amely adatbázis a későbbiekben bármelyik függvény belsejéből elérhető.

A UserController.php belsejében pedig az alábbi kódot helyezhetjük el:

public function indexAction()
 {
   $registry = Zend_Registry::getInstance();
  
   $title      = $registry['title'];     //Önállóan beírt stringet szedünk szét
   $credits    = $registry['credits'];
   $strCredit = implode(", ",$credits);  //A tömb tartalmát szedjük szét
  
   $this->view->assign('name', 'Wiwit');
   $this->view->assign('title', $title);
   $this->view->assign('credits', $strCredit); 
 }

A fenti kóddal természetesen az index.php-ban beírt adatokat lehet elérni. Ha a stringeket, a tömböket és az objektumokat is fel szeretnénk használni a UserController.php-ban, akkor az így fog kinézni:

<?php
require_once 'Zend/Controller/Action.php';
class UserController extends Zend_Controller_Action
{
  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);
  }
 
  public function nameAction()
  { 
     $request = $this->getRequest();
     $this->view->assign('name', $request->getParam('username'));
     $this->view->assign('gender', $request->getParam('gender'));  
     $this->view->assign('title', 'User Name');
  } 
 
  public function registerAction()
  {
     $request = $this->getRequest();
     $this->view->assign('action',"process");
     $this->view->assign('title','Member Registration');
     $this->view->assign('label_fname','First Name');
     $this->view->assign('label_lname','Last Name');
     $this->view->assign('label_uname','User Name');
     $this->view->assign('label_pass','Password');
     $this->view->assign('label_submit','Register'); 
     $this->view->assign('description','Please enter this form completely:'); 
  }
 
  public function editAction()
  {
     $registry = Zend_Registry::getInstance(); 
     $DB = $registry['DB'];
     $request = $this->getRequest();
     $id   = $request->getParam("id");

     $sql = "SeleCT * FROM `user` WHERE id='".$id."'";
     $result = $DB->fetchRow($sql);

     $this->view->assign('data',$result);
     $this->view->assign('action', $request->getBaseURL()."/user/processedit");
     $this->view->assign('title','Member Editing');
     $this->view->assign('label_fname','First Name');
     $this->view->assign('label_lname','Last Name');
     $this->view->assign('label_uname','User Name');
     $this->view->assign('label_pass','Password');
     $this->view->assign('label_submit','Edit'); 
     $this->view->assign('description','Please update this form completely:'); 
  } 
  public function processAction()
  {
     $registry = Zend_Registry::getInstance(); 
     $DB = $registry['DB'];
     $request = $this->getRequest();
     $data = array('first_name' => $request->getParam('first_name'),
                   'last_name' => $request->getParam('last_name'),
                   'user_name' => $request->getParam('user_name'),
                   'password' => md5($request->getParam('password'))
               );
     $DB->insert('user', $data);

     $this->view->assign('title','Registration Process');
     $this->view->assign('description','Registration succes');  
  }
 
  public function listAction()
  {
    $registry = Zend_Registry::getInstance(); 
    $DB = $registry['DB'];
    $sql = "SeleCT * FROM `user` ORDER BY user_name ASC";
    $result = $DB->fetchAssoc($sql);

    $this->view->assign('title','Member List');
    $this->view->assign('description','Below, our members:');
    $this->view->assign('datas',$result); 
  }
 
  public function processeditAction()
  {
    $registry = Zend_Registry::getInstance(); 
    $DB = $registry['DB'];
    $request = $this->getRequest();
    $data = array('first_name' => $request->getParam('first_name'),
               'last_name' => $request->getParam('last_name'),
               'user_name' => $request->getParam('user_name'),
               'password' => md5($request->getParam('password'))
               );
    $DB->update('user', $data,'id = '.$request->getParam('id'));
    $this->view->assign('title','Editing Process');
    $this->view->assign('description','Editing succes');  
  }
 
  public function delAction()
  {
    $registry = Zend_Registry::getInstance(); 
    $DB = $registry['DB'];
    $request = $this->getRequest();
    $DB->delete('user', 'id = '.$request->getParam('id'));
    $this->view->assign('title','Delete Data');
    $this->view->assign('description','Deleting succes');     
    $this->view->assign('list',$request->getBaseURL()."/user/list"); 
  }
}
?>

A fenti fájlban minden egyes adatbázissal foglalkozó függvény a registryből kapja a megfelelő paramétereket.