【问题标题】:Authentication between modules in Zend FrameworkZend Framework 中模块之间的身份验证
【发布时间】:2019-02-26 13:08:42
【问题描述】:

目前我正在使用带有两个模块的 ZF3。

第一个模块处理用户的身份验证
第二个模块应该使用有关用户身份验证状态的信息

详细说明:第二个模块应验证用户是否已登录。
根据该状态,它应该允许路由到某些控制器。

例如在第一个模块的模板中,我可以使用:$this->auth()->isLoggedIn()
但是 - 当然 - 我不能在第二个模块模板中使用 auth()。

我还在学习ZF,所以不知道如何通知第二个模块关于身份验证的状态。 我猜它与 ZFs ServiceManager 或 PluginManager 有关,但我不确定。

任何帮助将不胜感激......

【问题讨论】:

  • 你知道如何创建和注入服务和事件吗?另外:docs.zendframework.com/zend-authentication(如果第一个答案是“否”,那么您将面临挑战;-))。另外,请显示您尝试过的代码、任何错误等。但是,作为提示:您需要使用别名'AuthenticationService'AuthenticationService(通过ServiceManager(实例$container)在工厂中可用),例如$container->get('AuthenticationService'))。 gl & hf :)
  • 感谢您的提示!我解决了。

标签: authentication model-view-controller zend-framework zend-framework3


【解决方案1】:

感谢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 来自于学说,FooManager 是这个模型的管理者)

在 FooController.php 中,我将 authManager 添加到构造函数中

$this->authManager = $authManager;

现在我可以得到例如任何 FooAction 中的用户名! :)

$this->authManager->getIdentity()->username

(将“用户名”替换为 user_id 行的正确名称)

【讨论】:

    猜你喜欢
    • 2014-01-04
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    相关资源
    最近更新 更多