1

我在 ZF1 登录控制器中具有以下功能并且工作正常但无法移植到 ZF2 请帮助我将此功能与 zf2 一起使用

if ($this->_request->isPost() && $userForm->isValid($_POST)) {
        $data = $userForm->getValues();

    //set up the auth adapter
    // get the default db adapter
    $db = Zend_Db_Table::getDefaultAdapter();

    //create the auth adapter
    $authAdapter = new Zend_Auth_Adapter_DbTable($db, 'users','username', 'password');

    //set the username and password
    $authAdapter->setIdentity($data['username']);
    $authAdapter->setCredential(md5($data['password']));

    //authenticate
    $result = $authAdapter->authenticate();
    if ($result->isValid()) {

        // store the username, first and last names of the user
        $auth = Zend_Auth::getInstance();
        $storage = $auth->getStorage();
        $storage->write($authAdapter->getResultRowObject(
            array('username' , 'first_name' , 'last_name', 'role')));

        return $this->_forward('index');
    } else {
        $this->view->loginMessage = "Sorry, your username or
            password was incorrect";
    }
}
$this->view->form = $userForm;`enter code here`
4

1 回答 1

2

ZF2 中最简单的方法是使用ZfcUser模块进行用户登录和注册。

或者,您需要重构以使用Zend\Db具有与Zend_Db. 不幸的是,截至 ZF2 beta 3,Zend\Auth尚未更新以使用新Zend\Db组件,因此您需要对其进行大量返工或等到以后的版本。另外,请注意 ZF2 使用名称空间,因此您至少需要为此进行重构。

于 2012-03-17T06:51:25.633 回答