2012-03-16 14 views
1

にZendのフレームワーク1からの機能のアップグレード私はZF1のログインコントローラで、以下の機能を持っており、正常に動作しますが、できないZF2へのポートこの機能は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` 

答えて

2

でZF2で最も簡単な方法を動作するように私を助けてくださいユーザのログインと登録にZfcUserモジュールを使用することです。

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