• PHP Help PHP Newbies
  • Catchable fatal error: Argument 1 passed to Zend_Auth_Adapter_DbTable::__construct()

Trying to build up a login scrip and this should work however i get a persistent error that crashes the script. I am not quite sure what is causing this.

Notice: Undefined index: DB in /Sites/dev/application/controllers/LoginController.php on line 34

Catchable fatal error: Argument 1 passed to Zend_Auth_Adapter_DbTable::__construct() must be an instance of Zend_Db_Adapter_Abstract, null given, called in Sites/dev/application/controllers/LoginController.php on line 36 and defined in Sites/dev/library/Zend/Auth/Adapter/DbTable.php on line 128

<?php

/*
	Login Authentication method
*/
require_once 'Zend/Controller/Action.php';
require_once 'Zend/Auth.php';
require_once 'Zend/Auth/Adapter/DbTable.php';

class LoginController extends Zend_Controller_Action
{
    public function indexAction()
    {
    	/*
    		Function displays the login forum
    	*/
		$request = $this->getRequest();  
$this->view->assign('action', $request->getBaseURL()."/login/auth"); // Submit buttons action $this->view->assign('title', 'Login Form'); // Page Title $this->view->assign('username', 'User Name'); // Username $this->view->assign('password', 'Password'); // Password } public function authAction() { /* Login Authentication */ $request = $this->getRequest(); $registry = Zend_Registry::getInstance(); $auth = Zend_Auth::getInstance(); $db = $registry['DB']; $authAdapter = new Zend_Auth_Adapter_DbTable($db); $authAdapter->setTableName('users')->setIdentityColumn('username')->setCredentialColumn('password'); /* Pass data though validation */ $uname = $request->getParam('username'); $paswd = $request->getParam('password'); $authAdapter->setIdentity($uname); $authAdapter->setCredential(md5($paswd)); /* Authenticate the user */ $result = $auth->authenticate($authAdapter); if ($result->isValid()) { $data = $authAdapter->getResultRowObject(null,'password'); $auth->getStorage()->write($data); $this->_redirect('/index/welcome/'); } else { $this->_redirect('/login/'); } } } ?>

    The root cause would appear to be that there is no key named 'DB' in the $registry array set a few lines before that. Do a print_r($registry) to find out what the actual key is, and remember that they are case-sensitive. In the bigger scheme of things, you could add some error-checking to make sure it is set and not empty before making that call to Zend_Auth_Adapter_DbTable($db), and also consider using a try/catch block in order to catch any such exceptions and handle them more "gracefully."

      Write a Reply...