Module Zend框架2:路由器;解析为无效的控制器类或别名:";

Module Zend框架2:路由器;解析为无效的控制器类或别名:";,module,zend-framework2,router,Module,Zend Framework2,Router,我正在寻找有关ZF2路由器配置的帮助 我想在开发环境中将多个控制器添加到单个模块中,尽管最终必须弄清楚如何指定特定的路由 为此,我目前正在尝试使用MVC快速入门文档中指定的general purpose module.config。然而,这似乎不像ZF2文档所建议的那样执行 文件说明: Zendskletonapplication附带的“默认路由”可能 让你开始行动,这条路线基本上是 “/{module}/{controller}/{action}”,允许您指定: “/zend user/he

我正在寻找有关ZF2路由器配置的帮助

我想在开发环境中将多个控制器添加到单个模块中,尽管最终必须弄清楚如何指定特定的路由

为此,我目前正在尝试使用MVC快速入门文档中指定的general purpose module.config。然而,这似乎不像ZF2文档所建议的那样执行

文件说明:

Zendskletonapplication附带的“默认路由”可能 让你开始行动,这条路线基本上是 “/{module}/{controller}/{action}”,允许您指定: “/zend user/hello/world”。我们将在这里创建一条路由 出于说明目的,创建显式管线是 建议做法。”

但是,控制人/业主, /房东/住宅/索引,但不是/房东/沙箱/索引。Home和sandbox是控制器。沙盒已按照命名约定“SandboxController”命名。我的看法是,文档中的child_routes部分的代码可能需要某种修改,但我没有做

在沙盒实例中,我在404错误页面上得到了这个错误

房东\控制器\沙箱(解析为无效的控制器类或 别名:房东\控制器\沙箱)

我尝试将'lown\Controller\Sandbox'=>'lown\Controller\SandboxController'添加到可调用项,但这会产生错误

我的控制器如下所示:

return array(
'controllers' => array(
    'invokables' => array(
        'Landlord\Controller\Home' => 'Landlord\Controller\HomeController',

    ),
),
'router' => array(
    'routes' => array(
        'landlord' => array(
            'type'    => 'Literal',
            'options' => array(
                // Change this to something specific to your module
                'route'    => '/landlord',
                'defaults' => array(
                    // Change this value to reflect the namespace in which
                    // the controllers for your module are found
                    '__NAMESPACE__' => 'Landlord\Controller',
                    'controller'    => 'home',
                    '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(
        'landlord' => __DIR__ . '/../view',
    ),
),
);

如果有一个简单的方法直接配置URL,这也将是有用的,或者是一个非常好的教程,这也将是赞赏

这里的问题是,即使路由器试图路由到您添加的新控制器,您仍然需要为您添加的每个控制器添加可调用部分。所以你的路由器在这一点上看起来不错,但是如果你添加了一个新的AboutController,那么你需要修改配置的顶部,就像这样

return array(
'controllers' => array(
    'invokables' => array(
        'Landlord\Controller\Home' => 'Landlord\Controller\HomeController',
        'Landlord\Controller\About' => 'Landlord\Controller\AboutController',
    ),
),

只要找到答案。。。张贴在这里,它可能会帮助一些人。。。 我假设您已经遵循了zf2文档提供的album module的示例,现在添加了新的模块,如“Admin”,我添加了新的控制器,如Admin、news等

这是路由器设置

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

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

            ),
        ),
    ),
在这里添加您的控制器

'controllers' => array(
        'invokables' => array(
            'Admin\Controller\News' => 'Admin\Controller\NewsController',
            'Admin\Controller\Admin' => 'Admin\Controller\AdminController',

        ),
    ),
现在,您可以在模块中使用调用控制器,就像我的例子中一样

localhost/{foldername}/admin/news/index and localhost/{foldername}/admin/admin/index

谢谢

我也遇到了同样的情况,我找到了与前一篇文章相同的解决方案,但我认为这不是更好的方法

要在我的routeur中添加le“clientController”:

'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route' => '/',
                'defaults' => array(
                    'controller' => 'Fcm\Controller\Index',
                    'action' => 'index',
                ),
            ),
        ),
        'client' => array(
            'type' => 'Segment',
            'options' => array(
                'route' => '/client[/:action][/:id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id' => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Fcm\Controller\Client',
                    'action' => 'index',
                ),
            ),
        ),
    ),
),

按如下方式配置路由:-
模块:-用户
控制器:-索引控制器

'router' => array(
        'routes' => array(
            'users' => array(
                'type' => 'Literal',
                'options' => array(
                    // Change this to something specific toyour 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(
                    'default' => 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(
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),