1

このようなものを取得したかった(これはコントローラーからのものです)

$authService = $this->serviceLocator->get('auth_service');
if ($authService->hasIdentity()) {
    [...]
} else {
    [...]
}

ビュー ファイル (*.phtml) で、ログインまたはログアウトのリンクを表示できます...

4

2 に答える 2

2

フレームワークで既に使用可能なビュー ヘルパーがありidentity()ます。それを使用するには、認証サービス インスタンスを にマップする必要がありZend\Authentication\AuthenticationServiceますauth_service

<?php
return array(
    //..
    'service_manager' => array(
        'aliases' => array(
            'Zend\Authentication\AuthenticationService' => 'auth_service',
        ),
    ),
    // ..
);

ヘルパーにはパラメーターがなく、ID または null を返すだけなので、ユーザーが認証されているかどうかをテストする例として、ビューで ... を使用します。

<?php
if ($this->identity()) : ?>
    Logged In User
<?php else : ?>
    Guest
<?php endif; ?>
于 2013-06-09T20:12:06.630 に答える