カスタム認証サービスがあり、ZF2 で次のようにアクセスしました。
Application/view/layout/layout.phtml
$authenticationService = $this->getHelperPluginManager()
->getServiceLocator()
->get('AuthenticationService');
$currentIdentity = $authenticationService->getIdentity();
現在、Zend\ServiceManager#getServiceLocator()
は非推奨です。
ZF3 のビュー スクリプト (この場合はレイアウトの具体的) でサービスを利用できるようにする方法は?
解決策は、グローバル ビュー変数を次のように割り当てることですonBootstrap(...)
。
namespace Application;
use ...
class Module
{
public function onBootstrap(MvcEvent $e)
{
...
$serviceManager = $e->getApplication()->getServiceManager();
$viewModel = $e->getApplication()->getMvcEvent()->getViewModel();
$viewModel->authenticationService = $serviceManager->get('AuthenticationService');
}
...
}
別の (おそらくさらに良い/よりクリーンな) 解決策は、ViewHelper
. こちら もご覧ください。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加