Zend_Auth can be a complicated beast, as it allows you to define your own authentication methods using custom adapters. Natively, Zend Framework comes with 3 adapters that you can use, Zend_Auth_Adapter_DbTable, Zend_Auth_Adapter_Digest, and Zend_Auth_Adapter_Http. As with everything in Zend Framework, you can chose to use one of these as if, if they meet your needs, extend them to do exactly what you need done, or write your own. By extending the Zend_Auth_Adapter_Interface you can build your own custom adapters to authenticate against any backend service you choose. Zend Framework documentation provides excellent examples of how to create your own adapters.
While different adapters may have vastly different options and parameters, they all have one thing in common, they all return a Zend_Auth_Result when authenticate() is called. Zend_Auth_Result is the payload wrapper. With it you can tell if the authentication passed or failed, get any error codes or messages, and get the identity information. We will look at these methods in deeper detail later.
I'd like to say one final word about the Zend_Auth system before we dive into some code details. By default Zend_Auth uses the Zend_Session for persistent storage. You can override this and use your own custom storage solution by implementing Zend_Auth_Storage_Interface in your class. If you chose the default, Zend_Storage will create or use the Zend_Auth namespace in the session. If you are confused about this, skip back a few chapters where we discussed Zend_Session and namespaces in a bit more detail.
I've glossed over a lot of the functionality of Zend_Auth simply because it's not germane to our discussion and example. However, once you get going on your own real-world application, I'm sure you will investigate further. About the third time you think you have hit a show-stopping hurdle, only to find that Zend Framework either already supports the functionality you need, or you can easily extend it with a few lines; you'll really start to appreciate just how flexible Zend Framework is.
We will be implementing a simple auth scheme using Zend_Auth_Adapter_DbTable as our adapter. Digest and HTTP are both good choices but since we are already working with a database, we won't add unnecessary complexity to our simple application.
I should probably mention here that Zend_Auth implements a singleton pattern. This, of course, means that there can only be one instance of the class. To get a handle to that instance, you use the static method Zend_Auth::getInstance().
Was this article helpful?