0

getServiceConfig()のようなAuthenticationServiceオブジェクトを作成して Zend Auth を実装しましたModule.php

'AuthService' => function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter, 'tblUSRUsers', 'Email', 'Password', "MD5(?)");

                    $authService = new AuthenticationService();
                    $authService->setAdapter($dbTableAuthAdapter);
                    $authService->setStorage($sm->get('Application\Model\MyAuthStorage'));

                    return $authService;
                },

コントローラーアクションでは、このオブジェクトを取得しています

$this->getServiceLocator()
                    ->get('AuthService');

認証のために、次を使用して値を取得します

$authservice    = $this->getServiceLocator()->get('AuthService');
$arrUserDetails = $authservice->getIdentity();

これらの値は利用可能です。

しかし、問題はServiceLocatorController コンストラクターで使用できないため、上記のコードをそこに記述できないことです。すべてのアクションでこのコードを記述することは、良い習慣ではないようです。誰でもこれを手伝ってもらえますか?

4

2 に答える 2

0

いくつかの方法があります。Module クラスで ControllerConfig メソッドをオーバーロードできるため、好きなようにコントローラーをインスタンス化できます。または、認証を処理して ID を返すコントローラー プラグインを作成することもできます。

編集

これらのリンクを以前に提供する必要がありましたが、電話からはあまりアクセスできませんでした;)。

ControllerConfig を理解するには、このブログ投稿をご覧ください。Module クラスの getControllerConfig メソッドの途中まで説明があります。これは、Module クラスをインスタンス化するときに Module Manager が検索するメソッドであり、その Module 内の Controller のインスタンス化をカスタマイズできます。Controller クラスを返すと、Controller Manager は残りの Controller 依存関係 (プラグインなど) を注入します。

コントローラー プラグインは、すべてのコントローラーに挿入される小さなクラスであり、アクションから呼び出して一般的なコードを実行できます。コントローラー アクションで使用する$this->getServiceLocator()場合は、プラグインを使用しています。

于 2013-09-26T10:24:15.497 に答える
0

考えられる解決策は、ユーザーの資格情報を設定するルート イベント ハンドラーを用意することです。

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        $eventManager = $e->getApplication()
            ->getEventManager ();

        $eventManager->attach (
            MvcEvent::EVENT_ROUTE,
            function  (MvcEvent $e)
            {
                $auth = $e->getApplication()
                          ->getServiceManager()
                          ->get('AuthService');

                $e->setParam('userinfo', $auth->getUserInfo());
                // update 
                $layout = $e->getViewModel();
                $layout->userinfo = $auth->getUserInfo();           
       });
    }

次に、次のようにコントローラーにアクセスします。

class IndexController extends AbstractActionController
{
    public function indexAction ()
    {
        $this->getEvent()->getParam('userinfo');
于 2013-09-26T10:36:18.330 に答える