【问题标题】:Zend framework 2- more than one RESTful api modulesZend framework 2-多于一个RESTful api模块
【发布时间】:2016-01-28 14:36:49
【问题描述】:

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

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

    例如,如果我在“Restful”模块之后保留“Testservices”模块,则“Testservices”正在工作。 示例 - Testservices:http://localhost/dev/public/testservices/Userslist 工作正常。

如果我打电话给这个http://localhost/dev/public/restful/stateslist 得到以下错误:

发生 404 错误 网页未找到。 请求的控制器无法分派请求。 控制器: 应用程序\控制器\索引

  1. 如果我在“Testservices”模块之后保留“Restful”模块,则“Restful”正在工作。得到与上述相反的错误。

这里是 application.config.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(),

);

这里是 Restful 模块的 module.config.php

  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'
    )
),

);

这里是 Testservices 模块的 module.config.php

  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'
    )
),

);

提前感谢您的帮助。

【问题讨论】:

    标签: php rest zend-framework2


    【解决方案1】:

    您的两条路线都使用 module.config.php 中的关键 api,我假设当所有这些都合并在一起时,最后一个加载的获胜。更改其中之一:

    '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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-03
      • 2017-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-13
      相关资源
      最近更新 更多