Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use it as a plugin, the only downside is that if you initialize the plugin in your bootstrap, then the plugin will be executed for every controller and action, since it would have to run before your controller.</p> <p>You could extend Zend_Auth and add extra methods to set up the auth adapter and manage the storage, and then you can just call Your_Custom_Auth::getInstance() to get the auth instance and then you can check for auth in the preDispatcth() part of your controllers that need auth.</p> <p>This way you can easily work with zend_auth in multiple places with less code</p> <pre><code>&lt;?php class My_User_Authenticator extends Zend_Auth { protected function __construct() {} protected function __clone() {} public static function getInstance() { if (null === self::$_instance) { self::$_instance = new self(); } return self::$_instance; } // example using zend_db_adapter_dbtable and mysql public static function getAdapter($username, $password) { $db = Zend_Controller_Front::getInstance() -&gt;getParam('bootstrap') -&gt;getResource('db'); $authAdapter = new Zend_Auth_Adapter_DbTable($db, 'accounts', 'username', 'password'); $authAdapter-&gt;setIdentity($username) -&gt;setCredential($password) -&gt;setCredentialTreatment( 'SHA1(?)' ); return $authAdapter; } public static function updateStorage($storageObject) { self::$_instance-&gt;getStorage()-&gt;write($storageObject); } } // in your controllers that should be fully protected, or specific actions // you could put this in your controller's preDispatch() method if (My_User_Authenticator::getInstance()-&gt;hasIdentity() == false) { // forward to login action } // to log someone in $auth = My_User_Authenticator::getInstance(); $result = $auth-&gt;authenticate( My_User_Authenticator::getAdapter( $form-&gt;getValue('username'), $form-&gt;getValue('password')) ); if ($result-&gt;isValid()) { $storage = new My_Session_Object(); $storage-&gt;username = $form-&gt;getValue('username'); // this object should hold the info about the logged in user, e.g. account details My_User_Authenticator::getInstance()-&gt;updateStorage($storage); // session now has identity of $storage // forward to page } else { // invalid user or pass } </code></pre> <p>Hope that helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload