php - Zend 框架 2 中的 Zend\ServiceManager\Exception\ServiceNotFoundException

标签 php zend-framework2

我已经在我的系统上安装了 Zf2 框架。
它的工作 Localhost 骨架 zend 应用程序示例页面。
当我在我的项目中插入新模块时出现错误:

Zend\ServiceManager\Exception\ServiceNotFoundException

   Zend\Mvc\Controller\ControllerManager::createFromInvokable: failed retrieving "userscontrollerindex(alias: Users\Controller\Index)" via invokable class "Users\Controller\IndexController"; class does not exist

#0 D:\xampp\htdocs\CommunicationApp\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php(637): Zend\ServiceManager\AbstractPluginManager->createFromInvokable('userscontroller...', 'Users\\Controlle...')
#1 D:\xampp\htdocs\CommunicationApp\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php(593): Zend\ServiceManager\ServiceManager->doCreate('Users\\Controlle...', 'userscontroller...')
#2 D:\xampp\htdocs\CommunicationApp\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php(525): Zend\ServiceManager\ServiceManager->create(Array)
#3 D:\xampp\htdocs\CommunicationApp\vendor\zendframework\zendframework\library\Zend\ServiceManager\AbstractPluginManager.php(103): Zend\ServiceManager\ServiceManager->get('Users\\Controlle...', false)
#4 D:\xampp\htdocs\CommunicationApp\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\ControllerManager.php(137): Zend\ServiceManager\AbstractPluginManager->get('Users\\Controlle...', Array, false)
#5 D:\xampp\htdocs\CommunicationApp\vendor\zendframework\zendframework\library\Zend\Mvc\DispatchListener.php(96): Zend\Mvc\Controller\ControllerManager->get('Users\\Controlle...')
#6 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#7 D:\xampp\htdocs\CommunicationApp\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#8 D:\xampp\htdocs\CommunicationApp\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#9 D:\xampp\htdocs\CommunicationApp\vendor\zendframework\zendframework\library\Zend\Mvc\Application.php(313): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#10 D:\xampp\htdocs\CommunicationApp\public\index.php(17): Zend\Mvc\Application->run()
#11 {main}

这里附上我的代码:

模块.config.php

<?php
return array(
 'controllers' => array(
 'invokables' => array(
            'Users\Controller\Index' =>
             'Users\Controller\IndexController',
             ),
            ),
 'router' => array(
      'routes' => array(
            'users' => array(
               'type' => 'Literal',
                   'options' => array(
        // Change this to something specific to your module
                              'route' => '/users',
                     'defaults' => array(
 // Change this value to reflect the namespace in which
 // the controllers for your module are found
               '__NAMESPACE__' => 'Users\Controller',
              'controller' => 'Index',
              'action' => 'index',
           ),
          ),
  'may_terminate' => true,
  'child_routes' => array(
 // This route is a sane default when developing a module;
 // as you solidify the routes for your module, however,
 // you may want to remove it and replace it with more
 // specific routes.
 'default' => array(
          'type' => 'Segment',
     'options' => array(
              'route' =>
                 '/[:controller[/:action]]',
          'constraints' => array(
                       'controller' =>
                '[a-zA-Z][a-zA-Z0-9_-]*',
                   'action' =>
                 '[a-zA-Z][a-zA-Z0-9_-]*',
               ),
               'defaults' => array(
         ),
               ),
          ),
           ),
         ),
       ),
  ),
 'view_manager' => array(
 'template_path_stack' => array(
 'users' => __DIR__ . '/../view',
 ),
 ),
);

模块.php

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

 namespace Users;

use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;

class Module implements AutoloaderProviderInterface
{
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
            // if we're in a namespace deeper than one level we need to fix the \ in the path
                    __NAMESPACE__ => __DIR__ . '/src/' . str_replace('\\', '/' , __NAMESPACE__),
                ),
            ),
        );
    }

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

    public function onBootstrap(MvcEvent $e)
    {
        // You may not need to do this if you're doing it elsewhere in your
        // application
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
    }
}

应用程序.config.php

 <?php
return array(
    // This should be an array of module namespaces used in the application.
    'modules' => array(
        'Application',
         'Users',
    ),

    // 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 override 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(),
);

当我在我的浏览器上运行时收到错误

An error occurred
An error occurred during execution; please try again later.
Additional information:
Zend\ServiceManager\Exception\ServiceNotFoundException

最佳答案

问题可能出在您的 autoload_classmap.php 文件中。 尝试将文件编辑为

<?php
 return array('Users\Controller\IndexController' => __DIR__ . '/Controller/IndexController.php',);

如果成功,您的 IndexController.php 文件可能位于默认目录之外。建议您将 'Users\Controller' 文件夹的内容移动到 'Users\src\Controller',然后将 autoload_classmap.php 文件编辑回

关于php - Zend 框架 2 中的 Zend\ServiceManager\Exception\ServiceNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28472051/

相关文章:

php - 使用 ffmpeg 将 mp4 转换为 x264 时数据无效,但将 flv 转换为 x264 时很好

php - 当我尝试创建索引时,Docker + elasticsearch始终返回 “no alive nodes found in your cluster”

css - 无法在 ZF2 View 中获取 TextArea 类型的 TextArea

zend-framework2 - ZF2 动态表单过滤器/验证器

javascript - 如何在自调用的jquery函数中获取php值?

PHP mySQL,将内容插入到两个表中

php - 如何在没有表单的情况下使用 Post 方法

javascript - Laravel - PHP array_push 为 Ajax 生成编号索引

authentication - ZF2认证

php - ZF2 中同一模块下的多个 namespace