Zend Framework 2 - 多个 RESTful API 模块


Zend framework 2- more than one RESTful api modules

我们有两个以上的RESTful模块(Restful&Testservices)。一次只有一个 REST 模块在工作。

  1. 如果我在 application.config 中重新排列模块顺序.php ,REST api 的最后一个模块正在工作。

    例如,如果我在"Restful"模块之后保留"测试服务"模块,则"测试服务"正在工作。示例 - 测试服务:http://localhost/dev/public/testservices/Userslist 工作正常。

如果我调用此 http://localhost/dev/public/restful/stateslist 收到以下错误:

发生 404 错误找不到页面。请求的控制器无法调度请求。控制器: 应用程序''控制器''索引

    如果我保留
  1. ,如果我在"测试服务"模块之后保留"Restful"模块,则"Restful"正在工作。得到的错误与上面相反。

这是应用程序配置.php

return array(
// This should be an array of module namespaces used in the application.
'modules' => array(
    'DoctrineModule',
    'DoctrineORMModule',
    'Application',
    'Ads',
    'Consumer',
    'Restful',
    'Cron',
    'Admin',
    'Payment',
    'Frontoffice',
    'Onboarding',
    'Testservices',
),
// These are various options for the listeners attached to the ModuleManager
'module_listener_options' => array(
    // This should be an array of paths in which modules reside.
    // If a string key is provided, the listener will consider that a module
    // namespace, the value of that key the specific path to that module's
    // Module class.
    'module_paths' => array(
        './module',
        './vendor',
    ),
    // An array of paths from which to glob configuration files after
    // modules are loaded. These effectively overide configuration
    // provided by modules themselves. Paths may use GLOB_BRACE notation.
    'config_glob_paths' => array(
        'config/autoload/{,*.}{global,local}.php',
    ),
// Whether or not to enable a configuration cache.
// If enabled, the merged configuration will be cached and used in
// subsequent requests.
//'config_cache_enabled' => $booleanValue,
// The key used to create the configuration cache file name.
//'config_cache_key' => $stringKey,
// Whether or not to enable a module class map cache.
// If enabled, creates a module class map cache which will be used
// by in future requests, to reduce the autoloading process.
//'module_map_cache_enabled' => $booleanValue,
// The key used to create the class map cache file name.
//'module_map_cache_key' => $stringKey,
// The path in which to cache merged configuration.
//'cache_dir' => $stringPath,
// Whether or not to enable modules dependency checking.
// Enabled by default, prevents usage of modules that depend on other modules
// that weren't loaded.
// 'check_dependencies' => true,
),
    // Used to create an own service manager. May contain one or more child arrays.
    //'service_listener_options' => array(
    //     array(
    //         'service_manager' => $stringServiceManagerName,
    //         'config_key'      => $stringConfigKey,
    //         'interface'       => $stringOptionalInterface,
    //         'method'          => $stringRequiredMethodName,
    //     ),
    // )
    // Initial configuration with which to seed the ServiceManager.
    // Should be compatible with Zend'ServiceManager'Config.
    // 'service_manager' => array(),

);

这是 module.config.php 用于 Restful 模块

  namespace Restful;
return array(
'controllers' => array(
    'invokables' => array(
        'Restful'Controller'Stateslist' => 'Restful'Controller'StateslistController',
        'Restful'Controller'Citieslist' => 'Restful'Controller'CitieslistController',
    ),
),
'router' => array(
    'routes' => array(
        'api' => array(
            'type' => 'Literal',
            'options' => array(
                'route' => '/Restful',
                'defaults' => array(
                    '__NAMESPACE__' => 'Restful'Controller',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'default' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '/[:controller]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*'
                        ),
                        'defaults' => array(
                        ),
                    ),
                ),
            ),
        ),
    ),
),
'view_manager' => array(
    'template_path_stack' => array(
        'api' => __DIR__ . '/../view',
    ),
    'strategies' => array(
        'ViewJsonStrategy'
    )
),

);

这是 module.config.php 用于 Testservices 模块

  namespace Testservices;
 return array(
'controllers' => array(
    'invokables' => array(
        'Testservices'Controller'Userslist' => 'Testservices'Controller'Userslist',
        'Testservices'Controller'Roleslist' => 'Testservices'Controller'RoleslistController',
    ),
),
'router' => array(
    'routes' => array(
        'api' => array(
            'type' => 'Literal',
            'options' => array(
                'route' => '/Testservices',
                'defaults' => array(
                    '__NAMESPACE__' => 'Testservices'Controller',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'default' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '/[:controller]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*'
                        ),
                        'defaults' => array(
                        ),
                    ),
                ),
            ),
        ),
    ),
),
'view_manager' => array(
    'template_path_stack' => array(
        'api' => __DIR__ . '/../view',
    ),
    'strategies' => array(
        'ViewJsonStrategy'
    )
),

);

提前感谢您的帮助。

您的两条路由都使用 module.config 中的密钥 api.php 我假设当这一切合并在一起时,最后一个加载的路由获胜。更改其中之一,例如:

'router' => array(
    'routes' => array(
        'api_changed' => array(
            'type' => 'Literal',
            'options' => array(
                'route' => '/Testservices',
                'defaults' => array(
                    '__NAMESPACE__' => 'Testservices'Controller',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'default' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '/[:controller]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*'
                        ),
                        'defaults' => array(
                        ),
                    ),
                ),
            ),
        ),
    ),
),

API 已更新为api_changed的位置