简体   繁体   中英

ZF2 getServiceConfig() from another module

So, I have an Admin module which houses all the Controllers such as Users, Usergroups, Products etc. In the Module.php there is the getServiceConfig() method which sets all the appropriate database tables for the controllers. This is fine.

My problem is with the Models (User.php and UserTable.php, for ex.). I ideally want these to come from their own independent modules so the Admin module is not completely overwhelmed with files.

When I write Module.php like this:

<?php

namespace Admin;

use User\Model\User;
use User\Model\UserTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;

class Module implements AutoloaderProviderInterface {

//Some other methods

public function getServiceConfig() {
    return array(
        'factories' => array(
            'User\Model\UserTable' => function($sm) {
        $tableGateway = $sm->get('UserTableGateway');
        $table = new UserTable($tableGateway);
        return $table;
    },
            'UserTableGateway' => function($sm) {
        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
        $resultSetPrototype = new ResultSet();
        $resultSetPrototype->setArrayObjectPrototype(new User());
        return new TableGateway('user', $dbAdapter, null, $resultSetPrototype);
    },

I get a blank page with dies not executing after it tries to initialize UserTable and User in getServiceConfig method. So, it is definitely a problem with those. It works fine if these classes are in the Admin module.

Ctrl-clicking the classes work so it is finding them. Is there any way to call these from the Admin's Module.php ? Thanks.

try to use it simply like below

class Module
{

    public function getServiceConfig() {
        return array(
            'factories' => array(
                'User\Model\UserTable' => function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table = new \User\Model\UserTable($dbAdapter);
                    return $table;
                },
            ),
        );

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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