【问题标题】:Zend Framework 2: Router "resolves to invalid controller class or alias:"Zend Framework 2:路由器“解析为无效的控制器类或别名:”
【发布时间】:2015-11-29 18:20:08
【问题描述】:

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

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

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

http://framework.zend.com/manual/2.0/en/modules/zend.mvc.quick-start.html

文档说明:

“ZendSkeletonApplication 带有一个“默认路由”,可能 让你采取这个行动。那条路线基本上预计 “/{module}/{controller}/{action}”,允许您指定: “/zend-用户/你好/世界”。我们将主要在这里创建一条路线 出于说明目的,创建显式路由是 推荐的做法。”

但是,控制器渲染 /landlord, /landlord/home/index,但不是 /landlord/sandbox/index。 Home 和沙盒是控制器。沙盒已按照命名约定“SandboxController”进行命名。我的看法是,文档中代码的 child_routes 部分可能需要一些我错过的修改。

在沙盒实例中,我在 404 错误页面上收到此错误。

Landlord\Controller\Sandbox(解析为无效的控制器类或 别名:地主\控制器\沙盒)

我尝试将 'Landlord\Controller\Sandbox' => 'Landlord\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 的简单方法,这也会很有用,或者非常好的教程也将不胜感激。

【问题讨论】:

    标签: module zend-framework2 router


    【解决方案1】:

    这里的问题是,即使路由器会尝试路由到您添加的新控制器,您仍然需要为您添加的每个控制器添加 Invokable 部分。因此,此时您的路由器似乎很好,但是如果您添加例如新的 AboutController,那么您将需要像这样修改配置的顶部...

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

    【讨论】:

      【解决方案2】:

      只要找到答案......在这里发布,它可能会帮助一些人...... 我假设您已经按照 zf2 docs 提供的相册模块示例,现在添加新模块,例如在我的情况下为“管理员”,并且我添加了新控制器,如管理员、新闻等,

      这是路由器设置...

      '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
      

      谢谢

      【讨论】:

        【解决方案3】:

        我也是这种情况,找到了和上一篇一样的解决办法,但我觉得不是更好的办法。

        为了在我的路由中添加“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',
                        ),
                    ),
                ),
            ),
        ),
        

        【讨论】:

          【解决方案4】:

          像这样配置路由:-
          模块:- 用户
          控制器:- 索引控制器

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-07-26
            • 1970-01-01
            相关资源
            最近更新 更多