Zend Framework 2 routing error: resolves to invalid controller class or alias

Zend Framework 2 routing error: resolves to invalid controller class or alias

我正在尝试学习 Zend Framework 2,我已经启动了他们的框架应用程序 运行。为了访问它,我访问了 http://localhost:8080/. When visiting that link it displays their generic Zend page. What I want to be able to do is visit http://localhost:8080/application/test 并让它将我带到具有不同布局的不同页面。

这里是module.config.php

<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication 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
 */

return array(
    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action'     => 'index',
                    ),
                ),
            ),
            // The following is a route to simplify getting started creating
            // new controllers and actions without needing to create a new
            // module. Simply drop new controllers in, and you can access them
            // using the path /application/:controller/:action
            'application' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/application',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Application\Controller',
                        'controller'    => 'Index',
                        'action'        => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    '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(
                            ),
                        ),
                    ),
                ),
            ),
        )
    ),
    'service_manager' => array(
        'abstract_factories' => array(
            'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
            'Zend\Log\LoggerAbstractServiceFactory',
        ),
        'aliases' => array(
            'translator' => 'MvcTranslator',
        ),
    ),
    'translator' => array(
        'locale' => 'en_US',
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Application\Controller\Index' => 'Application\Controller\IndexController'
        ),
    ),
    'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => array(
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),
    // Placeholder for console routes
    'console' => array(
        'router' => array(
            'routes' => array(
            ),
        ),
    ),
);

为了让它对我有用,我尝试了以下方法:

首先我在 Application/src/Application/Controller/.

中创建了一个名为 TestController 的控制器

接下来我添加了 'application/test/index' => DIR 。 '/../view/application/test/index.phtml' 到 view_manager.

中的 template_map 数组

我还添加了 'Application\Controller\Test' => 'Application\Controller\TestController' 到控制器数组。

当我访问 http://localhost:8080/application/test 时出现错误: 测试(解析为无效控制器 class 或别名:测试)

我显然做错了什么,但是 Zend 上的文档对新手来说一点都不友好,而且它变得非常令人沮丧。有人能指出我正确的方向吗?配置太多,我确定我遗漏了一些小东西。谢谢!

目前名为 application 的路由(父级)定义了 /application 的 URL 路由。然而,子路由 default 需要将控制器名称作为第一个参数传入,然后是 动作。

这意味着 URL 将是

/application/[:controller]/[:action]

所以来访

/application/test

您无意中试图获取 'test' 控制器;因此错误。

Resolves to invalid controller class or alias : test

为了解决这个问题,我强烈建议不要使用 :controller 路由参数,而是使用路由 per 控制器操作。

'application' => [
    'type' => 'Literal',
    'options' => [
        'route' => '/application',
        'defaults' => [
            'controller' => 'Application\Controller\Index',
            'action'     => 'index',
        ],
        'may_terminate' => true,
        'child_routes' => [

            'test' => [
                'type' => 'Literal',
                'options' => [
                    'route' => '/test',
                    'defaults' => [
                        // controller value is inherited from parent!
                        'action' => 'test', 
                    ],
                ],
            ],

        ],
    ],
],