views:

51

answers:

2

Hi,

i have created a controller in which the default index action is being used to display a login form and then authenticate the user. However i have ended up having to add functions into the controller which i feel will clutter up the controller.

for example i have functions like:

 protected function _process($values)
    {
        // Get our authentication adapter and check credentials
        $adapter = $this->_getAuthAdapter();
        $adapter->setIdentity($values['username']); 
        $adapter->setCredential($values['password']);

        $auth = Zend_Auth::getInstance();
        $result = $auth->authenticate($adapter);
        if ($result->isValid()) {
            $user = $adapter->getResultRowObject();
            $auth->getStorage()->write($user);
            return true;
        }
        return false;
    }

   protected function _getAuthAdapter() {

        $dbAdapter = Zend_Db_Table::getDefaultAdapter();
        $authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);

        $authAdapter->setTableName('users')
            ->setIdentityColumn('username')
            ->setCredentialColumn('password')
            ->setCredentialTreatment('SHA1(CONCAT(?,salt))');

                return $authAdapter;
    }

What would you recommend to do, maybe create another directory called custom_classes and include the file into my controller this way?

A: 

If the methods you add are necessary for the controller to handle the input it receives from the User Interface, then it's fine to have them there. If it's a different concern, identify which responsibility it is and add the method there.

If you find the logic in the methods is useful in many controllers, consider making the logic into a Zend_Controller_Action_Helper. If you find this is that has to run on every request but is not directly related to a Controller Action, make it into a Zend_Controller_Plugin.

In case of authenticating users, you might want to create a Zend_Controller_Plugin that authenticates the user before the actual Controller Action is called.

Gordon
A: 

Given your example I would put this into some sort of ACL centered Zend_Controller_Plugin class and register this class in your bootstrap to ensure it is always run.

Also, depending on what you are doing, the logic could go into one of your Models.

Models should represent a collection of information that make up an entity of some sort, the Model class should also be responsible for reading, updating, removing and adding new Models.

For example a User Model could represent a User in a table in your database. It might contain functions like updateFailedLogins(), updateLogins() and specific functions related to the log in process for that particular User Model.

jakenoble