モジュールに2つのコントローラがあり、両方ともログインしているかどうかを確認する必要があります。ログインコントローラは、DbTableを使用してユーザーを認証し、そのIDをストレージに書き込みます。zend framework 2 AuthenticationService
私は> Zend \ Authentication \ AuthenticationServiceを使用しています。 $ auth = new AuthenticationService();このため、コントローラの関数内
しかし、私は(複数pageActionにそのインスタンスをインスタンス化)
public function getServiceConfig()
{
return array(
'factories' => array(
'Application\Config\DbAdapter' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
return $dbAdapter;
},
'Admin\Model\PagesTable' => function($sm){
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$pagesTable = new PagesTable(new TableGateway('pages',$dbAdapter));
return $pagesTable;
},
'Admin\Authentication\Service' => function($sm){
return new AuthenticationService();
}
),
);
}
することができますように、次のように私はModule.php
に関数を書きました私が悪いと思うたびに新しいAuthenticationService()を返すのを見てください。私はすでにインスタンス化されたサービスのインスタンスを取得する方法を見つけることができませんでした。または これのためにシングルトンクラスを作成する必要があります。より深い説明でサンプルコードスニペットをアドバイスしていただければ幸いです。
public function getServiceConfig()
{
return array(
'aliases' => array(
'Application\Config\DbAdapter' => 'Zend\Db\Adapter\Adapter',
'Admin\Authentication\Service' => 'Zend\Authentication\AuthenticationService',
),
'factories' => array(
'Admin\Model\PagesTable' => function ($serviceManager) {
$dbAdapter = $serviceManager->get('Application\Config\DbAdapter');
$tableGateway = new TableGateway('pages', $dbAdapter);
$pagesTable = new PagesTable($tableGateway);
return $pagesTable;
},
),
);
}
注意は主に根の配列の「エイリアス」セクションには、その他の変更は単なる化粧品であり、あなたは、このような工場を使用して(あなたが提案し、元の道を行うことを好むことがあります。
またはコースのISE
、あなたはモジュール特定のサービスを持ってしたくない場合は、あなただけの$ serviceManager-を使用することができます>( 'Zendの\認証\ AuthenticationService')を取得; – ise
ありがとうございました@iseこれは私が探していたものです。 –