Using the registry
So far, you've covered how to make all of this work, but there's one more feature you need to look at that will make your life easier: the Zend_Registry.
The Zend_Registry is a place to store all of those objects you're going to use repeatedly in your application. For example, if you have a view that you use in multiple places, storing it here will enable you to have a single instance you can change without having to find all of the instances throughout replication.
The registry can only store objects and stores them with a string value name by which you can later retrieve them. You may not remove an object from the registry, but understand that it exists only in the context of your current request, anyway. It is not a means for achieving persistent storage of objects, but, rather, a way to avoid storing them in session variables.
In your case, you can see it in action by storing the database connection in the registry, avoiding the necessity of recreating it every time you want to access the database.
Because all requests flow through the index.php file, that's the most likely place for us to create the database connection and register (see Listing 27).
Listing 27. Creating the database connection
<?php
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Db');
Zend_Loader::loadClass('Zend_Controller_Front');
Zend_Loader::loadClass('Zend_Registry');
$params = array(
'host' => 'localhost',
username' => 'chompuser',
'password' => 'chomppassword',
dbname' => 'chomp'
);
$db = Zend_Db::factory('Pdo_Mysql', $params);
Zend_Registry::set('db', $db);
$baseURL = str_replace('/index.php','/',$_SERVER['PHP_SELF']);
Zend_Registry::set('baseURL', $baseURL);
$front = Zend_Controller_Front::getInstance();
$front->setBaseUrl($baseURL);
$front->setControllerDirectory('../application/controllers');
$front->setParam('noViewRenderer', true);
$front->dispatch();
?>
|
Here, you simply create the database connection as usual, adding it to the registry using the register() function and assigning it a name
of DB. Now, if you need to change a username,
password, or other information, simply change it here, and you're
done. You will probably want to put this information in a less-vulnerable file and simply include it.
Accessing the registered object
Once the database connection has been registered, you can access it
easily. At the moment, the only file that uses the database is the UserController (see Listing 28).
Listing 28. Accessing the registry
<?php
require_once 'Zend/Db.php';
Zend_Loader::loadClass('Zend_Controller_Action');
Zend_Loader::loadClass('Zend_View');
class UserController extends Zend_Controller_Action
{
...
function authenticateAction()
{
$DB = Zend_Registry::get('db');
$select = $DB->select();
$select->from('users', '*');
$select->where('Username = ?', $_POST['username']);
$select->where('Password = ?', $_POST['password']);
$sql = $select->__toString();
$rowsFound = $DB->fetchAll($select);
if (isset($rowsFound[0]['username']))
{
session_start();
$_SESSION['RSS_SESSION'] = session_id();
$_SESSION['firstname'] = $rowsFound[0]['firstname'];
$_SESSION['username'] = $rowsFound[0]['username'];
echo "Login successful. Please <a href='/'>continue</a>.";
}
else
{
echo "Login information incorrect. \
Please <a href='/user/login'>try again</a>.";
}
}
function logoutAction()
{
echo 'This is the logoutAction.';
}
function registerAction()
{
$view = new Zend_View();
$view->setScriptPath('../application/views/');
echo $view->render('register.php');
}
function createAction()
{
$DB = Zend_Registry::get('db');
$row = array(
'FirstName' => $_POST['fName'],
'LastName' => $_POST['lName'],
'EmailAddress' => $_POST['email'],
'Username' => $_POST['user'],
...
|
Notice that all you have done here is replace the database connection
information with a call to the registry asking for the object
reregistered under the string DB. Once you have the object, you use it just as you used it before.




