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または、とは非常に異なるAPIを持つ新しいコンポーネントを使用するようにリファクタリングする必要がありますZend_Db。残念ながら、ZF2ベータ3の時点でZend\Authは、新しいコンポーネントを使用するようにまだ更新されていないZend\Dbため、かなり手直しするか、後のリリースまで待つ必要があります。また、ZF2は名前空間を使用するため、少なくともそのためにリファクタリングする必要があることに注意してください。

于 2012-03-17T06:51:25.633 に答える