Zend Framework2 会员管理项目之二:创建会员模块以及模块配置

2014年03月02日

zf2的特点之一处就是模块化,这很便于我们团队协作,项目组成员可以在单独完成自己的模块,然后整合。

创建Member模块

zf2创建模块的集中方法,请参考以下博客。

Zend Framework 2 添加模块

我们的根目录大概是这个样子的

2014-03-01-01

将Member模块展开大概是这个样子的

2014-03-01-02

修改根目录下config/application.config.php,添加Member

<?php
return array(
    // This should be an array of module namespaces used in the application.
    'modules' => array(
        'Application',
        'Member',  //添加这行,使Member模块有效
    ),

    // These are various options for the listeners attached to the ModuleManager
    'module_listener_options' => array(
        'module_paths' => array(
            './module',
            './vendor',
        ),

        'config_glob_paths' => array(
            'config/autoload/{,*.}{global,local}.php',
        ),
    ),
);

修改module/Member/module.php

添加getServiceConfig()函数,此函数会调用zf2的ServiceManager,注入必要的类,关于ServiceManager请参考这篇博客

<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonModule for the canonical source repository
 * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Member;

use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Member\Model\User;
use Member\Model\UserTable;

class Module implements AutoloaderProviderInterface
{
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . str_replace('\\', '/' , __NAMESPACE__),
                ),
            ),
        );
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function onBootstrap(MvcEvent $e)
    {
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
    }

    /**
     * get services
     *
     * @return array
     */
    public function getServiceConfig()
    {
        return array(
            'abstract_factories' => array(),
            'aliases' => array(),
            'factories' => array(
                // DB
                '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('users', $dbAdapter, null, $resultSetPrototype);
                },

                // FORMS
                'LoginForm' => function ($sm) {
                    $form = new \Member\Form\LoginForm();
                    $form->setInputFilter($sm->get('LoginFilter'));
                    return $form;
                },
                'RegisterForm' => function ($sm) {
                    $form = new \Member\Form\RegisterForm();
                    $form->setInputFilter($sm->get('RegisterFilter'));
                    return $form;
                },
                'UserCreateForm' => function ($sm) {
                    $form = new \Member\Form\UserCreateForm();
                    $form->setInputFilter($sm->get('UserCreateFilter'));
                    return $form;
                },
                'UserEditForm' => function ($sm) {
                    $form = new \Member\Form\UserEditForm();
                    $form->setInputFilter($sm->get('UserEditFilter'));
                    return $form;
                },
                // FILTERS
                'LoginFilter' => function ($sm) {
                    return new \Member\Form\LoginFilter();
                },
                'RegisterFilter' => function ($sm) {
                    return new \Member\Form\RegisterFilter();
                },
                'UserCreateFilter' => function ($sm) {
                    return new \Member\Form\UserCreateFilter();
                },
                'UserEditFilter' => function ($sm) {
                    return new \Member\Form\UserEditFilter();
                },
            ),
            'invokables' => array(),
            'services' => array(),
            'shared' => array(),
        );
    }
}