このようなものを取得したかった(これはコントローラーからのものです)
$authService = $this->serviceLocator->get('auth_service');
if ($authService->hasIdentity()) {
[...]
} else {
[...]
}
ビュー ファイル (*.phtml) で、ログインまたはログアウトのリンクを表示できます...
このようなものを取得したかった(これはコントローラーからのものです)
$authService = $this->serviceLocator->get('auth_service');
if ($authService->hasIdentity()) {
[...]
} else {
[...]
}
ビュー ファイル (*.phtml) で、ログインまたはログアウトのリンクを表示できます...
フレームワークで既に使用可能なビュー ヘルパーがあり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; ?>