ZF2 の認証サービスを使用しようとすると、いくつかの問題が発生します。Module.php getServiceConfig 関数に従う必要があります。
<?php
public function getServiceConfig()
{
return array(
'factories' => array(
'Auth\Model\CustomerTable' => function($sm) {
$tableGateway = $sm->get('CustomerTableGateway');
$table = new CustomerTable($tableGateway);
return $table;
},
'CustomerTableGateway' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Customer()); // prototype pattern implemented.
return new TableGateway('customer', $dbAdapter, null, $resultSetPrototype);
},
'Auth\Model\AuthStorage' => function($sm){
return new \Auth\Model\AuthStorage('jamietech');
},
'AuthService' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter,
'customer','username','password');
$authService = new AuthenticationService();
$authService->setAdapter($dbTableAuthAdapter);
$authService->setStorage($sm->get('Auth\Model\AuthStorage'));
return $authService;
},
),
);
}
AuthStorage ファクトリは、rememberMe 関数を追跡するために新しい AuthStorage を作成するだけで、AuthService ファクトリは新しい認証サービスを作成します。私が間違ったことは何もわかりませんが、AuthController.php で次のコードを実行すると:
<?php
public function loginAction()
{
//if already login, redirect to success page
if ($this->getAuthService()->hasIdentity()){
return $this->redirect()->toRoute('success');
}
$form = new LoginForm();
return array(
'form' => $form,
'messages' => $this->flashmessenger()->getMessages()
);
}
public function logoutAction()
{
$this->getSessionStorage()->forgetMe();
$this->getAuthService()->clearIdentity();
$this->flashmessenger()->addMessage("You have logged out successfully.");
return $this->redirect()->toRoute('auth', array('action'=>'login'));
}
PHPUnit コマンドを実行すると、PHPUnit で次のエラーが発生します。
1: "testLoginActionCanBeAccessed - Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance of Zend\Db\Adapter\Adapter
1: "testLogoutActionCanBeAccessed - session_regenerate_id(): cannot regenerate session id - headers already sent.
-process-isolation コマンドを実行すると、ログインとログアウトの両方で次のエラーが発生します。
"Serialization of closure is not allowed in: C;\Users\-----\AppData\Local\Temp\-----
誰かがそれを助けることができれば、それは素晴らしいことです. 私は ZF 初心者なので、あまり厳しくしないようにしてください。
編集: ところで、global.php ファイルには、ZF2 チュートリアル アプリケーションに示されている service_manager アダプター ファクトリが含まれています。
ありがとうございました!ジェイミー・マクラフラン