Php ZF2中的路由问题:控制器未映射

Php ZF2中的路由问题:控制器未映射,php,zend-framework2,zend-route,Php,Zend Framework2,Zend Route,我试图在Zf2中创建一个简单的CRUD来了解它,但我在路由我唯一的控制器时遇到了问题。我有这个错误 “请求的控制器无法映射到现有控制器类” 我正在尝试调用此路径:http://zf2.local/Listapp 这是我的结构: module/Listapp/src/Listapp/Controller/ListappController.php 名称空间是名称空间Listapp\Controller 这是我的自动加载器配置: public function getAutoloaderConfig

我试图在Zf2中创建一个简单的CRUD来了解它,但我在路由我唯一的控制器时遇到了问题。我有这个错误

“请求的控制器无法映射到现有控制器类”

我正在尝试调用此路径:
http://zf2.local/Listapp

这是我的结构:
module/Listapp/src/Listapp/Controller/ListappController.php

名称空间是
名称空间Listapp\Controller

这是我的自动加载器配置:

public function getAutoloaderConfig()
 {
     return array(
         'Zend\Loader\StandardAutoloader' => array(
             'namespaces' => array(
                 // Autoload Listapp classes
                 __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                 // Autoload ListappController classes
                 'ListappController' => __DIR__ . '/src/Listapp',
             )
         )
     );
 }
这是我的
module.config.php

return array(
 'controllers' => array(
     'invokables' => array(
         'Listapp\Controller\Listapp' => 'Listapp\Controller\ListappController'
     )
 ),

 'router' => array(
     'routes' => array(
         'listapp' => array(
             'type'    => 'segment',
             'options' => array(
                 'route'    => '/[:controller[/:action][/:id]]',
                 'constraints' => array(
                     'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                     'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                     'id'     => '[0-9]+',
                 ),
                 'defaults' => array(
                     'controller' => 'Listapp\Controller\Listapp',
                     'action'     => 'index',
                 ),
             ),
         ),
     ),
 ),

 'view_manager' => array(
     'template_path_stack' => array(
         'Listapp' => __DIR__ . '/../view',
     ),
 ), );
任何帮助都将不胜感激,谢谢

编辑: 这是控制器中的代码(减去其他CRUD函数):


因此,为了进一步解释我的评论,通过在路由中包含一个
:controller
段,您已经告诉ZF尝试将URL中的第一项内容与controller manager可以加载的内容相匹配(在您的情况下,controller invokables中的一个键)。仅当您访问
http://zf2.local/

因此,对您来说,最快的修复方法是将配置更改为:

'controllers' => array(
     'invokables' => array(
         'Listapp' => 'Listapp\Controller\ListappController'
     )
 ),
URL中的“Listapp”将与此控制器匹配,一切都将按照您的预期工作

一般来说,如果您避免在路由中使用
:controller
,并且每个controller至少有一条路由,则情况会更清楚,例如:

 'controllers' => array(
     'invokables' => array(
         'Listapp\Controller\Listapp' => 'Listapp\Controller\ListappController'
     )
 ),
 'router' => array(
     'routes' => array(
         'listapp' => array(
             'type'    => 'segment',
             'options' => array(
                 'route'    => '/listapp[/:action[/:id]]',
                 'constraints' => array(
                     'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                     'id'     => '[0-9]+',
                 ),
                 'defaults' => array(
                     'controller' => 'Listapp\Controller\Listapp',
                     'action'     => 'index',
                 ),
             ),
         ),
     ),
 ),

您可以发布Listapp代码吗?如果您将可调用项中的Listapp控制器键从
Listapp\controller\Listapp
更改为仅
Listapp
,是否有效?是的,它有效!谢谢!在尝试您的示例并删除
:controller
后,我出现以下错误:请求的URL无法通过路由进行匹配。在我的示例中,我确实将listapp的“L”改为小写(IMO URL通常应为小写),因此这可能是唯一的问题。试试
http://zf2.local/listapp
我同意Tim Fountain关于更明确路线定义的观点。skeleton应用程序建议使用magic route定义,以减少初学者需要配置的内容的数量,但我认为它具有暗示建议使用magic routes的效果。事实上,显式路由更易于管理和调试。
 'controllers' => array(
     'invokables' => array(
         'Listapp\Controller\Listapp' => 'Listapp\Controller\ListappController'
     )
 ),
 'router' => array(
     'routes' => array(
         'listapp' => array(
             'type'    => 'segment',
             'options' => array(
                 'route'    => '/listapp[/:action[/:id]]',
                 'constraints' => array(
                     'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                     'id'     => '[0-9]+',
                 ),
                 'defaults' => array(
                     'controller' => 'Listapp\Controller\Listapp',
                     'action'     => 'index',
                 ),
             ),
         ),
     ),
 ),