Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1
CHAPTER 16 ■ ADVANCED ZEND FRAMEWORK^245

Replacing get with post, cookie, session, or even files will result in distinct caching
scenarios. Remember, though, that this type of caching is appropriate only where the page’s
output is always the same for the same input parameters.

Authorizing Users


The Zend_Auth component allows you to confirm a visitor’s identity. This component combines
sessions and database access to create a robust identity system. To understand Zend_Auth, it’s
best to see it in a complete example.
Listing 16-10 shows a complete authentication solution, integrated with forms processing,
the FlashMessenger, and a database. The prerequisite for this example is a database table called
users, with fields named email, password, and name. Create the IndexController.php file as
shown in this listing.

Listing 16-10. A Complete Zend_Auth Example (IndexController.php)

//Builds on the Hello: name example
public function indexAction() {

//Get a reference to Zend_Auth
$auth = Zend_Auth::getInstance();

//Check to see if the user is logged in
if($auth->hasIdentity()) {
//Get the user's identity and set view variable to name
$identity = $auth->getIdentity();
$this->view->name = $identity->name;
} else {
//No identity = Welcome: unknown.
$this->view->name = 'unknown';
}
}

public function loginAction() {
if($this->getRequest()->isPost()) {

//Filter tags from the fields
$filters = array(
'email' => 'StringTrim',
'password' => 'StringTrim'
);

$validation = array(
'email'=>array('emailAddress'),
'password' => array (
array('StringLength', 1, 64)
)
);

McArthur_819-9C16.fm Page 245 Friday, February 29, 2008 5:07 PM

Free download pdf