简体   繁体   中英

Zend framework 2- more than one RESTful api modules

We have more than two RESTful modules (Restful & Testservices). Only one REST module is working at a time.

  1. If I rearranged the Modules order in application.config.php , the last module of REST api is working .

    For example if I keep 'Testservices' module after " Restful" module , "Testservices" is working. Exmple - Testservices : http://localhost/dev/public/testservices/Userslist is working fine .

If I am calling this http://localhost/dev/public/restful/stateslist getting the following error :

A 404 error occurred Page not found. The requested controller was unable to dispatch the request. Controller: Application\\Controller\\Index

  1. If I keep if I keep ' Restful ' module after " Testservices " module , " Restful " is working. Getting errors just reverse of above.

Here is the 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(),

);

Here is the module.config.php for Restful module

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

);

Here is the module.config.php for Testservices module

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

);

Thanks for your help in advance.

Both your routes use the key api in module.config.php I assume when this all gets merged together the last one loaded wins. Change one of them like:

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

Where api has been updated to api_changed

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