目前我正在使用带有两个模块的ZF3。
第一个模块处理用户的身份验证。 第二个模块应使用有关用户身份验证状态的信息。
详细信息:第二个模块应验证用户是否已登录。 根据该状态,它应允许路由到某些控制器。
例如。在第一个模块的模板中,我可以使用:$this->auth()->isLoggedIn()
但是 - 当然 - 我无法在第二个模块模板中使用auth()。
我还在学习ZF,所以我不知道如何通知第二个模块有关身份验证的状态。我想这与ZFs ServiceManager或PluginManager有关,但我不确定。
任何帮助,将不胜感激...
感谢https://www.tutorialspoint.com/zend_framework/zend_framework_service_manager.htm的一些解释,我解决了它!
在FooControllerFactory :: __ invoke中,我使用Container来查找AuthManager
$authManager = $container->get(\Vendor\Auth\Manager::class);
然后,我把它归还了:
return new FooController($entityManager, $FooManager, $authManager);
(entityManager来自doctrine,FooManager是此模型的管理者)
在FooController.php中,我将authManager添加到构造函数中
$this->authManager = $authManager;
现在我可以得到,例如任何FooAction中的用户名! :)
$this->authManager->getIdentity()->username
(将“username”替换为user_id行的正确名称)