简体   繁体   English

如何在ZF2模块之间共享模型

[英]how to share model between ZF2 modules

I'm beginner in ZF2 world and I have following problem. 我是ZF2世界的初学者,遇到以下问题。

I'm writing browser game and I planned to create main MVC module, related to game Page rendering (eg Oveview page, Buildings page etc.). 我正在编写浏览器游戏,并计划创建与游戏页面渲染(例如,Oveview页面,Buildings页面等)相关的主要MVC模块。 Different pages are generated by different Page\\Controllers. 不同的页面由不同的页面\\控制器生成。 Everything fine. 一切都很好。

But I also planned to add some engine-specific modules, eg User, Planet, Queue, which should implement only Model side of MVC (mean no View and no Controller). 但是我还计划添加一些引擎特定的模块,例如User,Planet,Queue,它们应该仅实现MVC的Model端(即无View,无Controller)。 Simply implement Model to access database and share it with main MVC Page module, which render game pages. 只需实现模型即可访问数据库并与主要MVC页面模块共享数据库,从而渲染游戏页面。

I invented the way to put engine-specific module API into serviceManager, so Page controllers can access it (guess its common way for ZF2). 我发明了将引擎特定的模块API放入serviceManager的方法,以便Page控制器可以访问它(猜测ZF2的通用方法)。 For example I have User interface : 例如我有用户界面:

interface UserInterface
{
    public function Create ();
    public function Load ($player_id);
    public function Login ();
    public function Logout ();
}

and UserManager class which implements it : 和实现它的UserManager类:

class UserManager implements UserInterface
{
    ....

In my \\module\\User\\config\\module.config.php file I added factory, which shares UserManager to other modules : 在我的\\ module \\ User \\ config \\ module.config.php文件中,添加了factory,该工厂与其他模块共享UserManager:

return array (
    'service_manager' => array(
        'factories' => array(
            'UserManager' => function($sm) {                
                return new \User\UserManager ();
            },
        ),
    ),
);

Its okay. 没关系。 Page Controller now can access UserManager API by abusing serviceManager : 现在,页面控制器可以通过滥用serviceManager来访问UserManager API:

$user = $this->getServiceLocator()->get('UserManager');

But how I can share Module API between modules outside Controller ? 但是,如何在Controller外部的模块之间共享Module API?

For example my UserManager require to do some actions with PlanetManager. 例如,我的UserManager需要对PlanetManager进行一些操作。

In other words I need nice zend-way to implement cross-Model interaction. 换句话说,我需要很好的zend-way来实现跨模型的交互。 Thanks. 谢谢。

I am unsure on the design of the UserInterface (it looks more like an API for a UserController ). 我不确定UserInterface的设计(它看起来更像UserController的API)。

Nevertheless, if the UserManager is dependent on the PlanetManager , you can inject it as a dependency . 不过,如果UserManager依赖于PlanetManager ,则可以其作为依赖项 注入

The easiest way would be to create a new factory, just as you have with the UserManager . 就像使用UserManager一样,最简单的方法是创建一个新工厂。

PlanetManager PlanetManager

class PlanetManager
{
  protected $userManager;

  public function __construct(UserManager $userManager)
  {
    $this->setUserManager($userManager);
  }

  public function setUserManager(UserManager $userManager);  
}

Module.php Module.php

return array (
    'service_manager' => array(
        'factories' => array(
            'UserManager' => function($sm) {                
                return new \User\UserManager ();
            },
            'PlanetManager' => function($sm) {
                $userManager = $sm->get('UserManager');

                return new PlanetManager($userManager);
            }
        ),
    ),
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM