Zend Framework 2路由错误:解析为无效的控制器类或别名

时间:2015-06-04 21:18:13

标签: zend-framework routing zend-framework2 zend-framework-mvc zend-framework-routing

我正在尝试学习Zend Framework 2,我已经启动并运行了他们的骨架应用程序。为了访问它,我访问了http://localhost:8080/。访问该链接时,它会显示其通用Zend页面。我希望能够做的是访问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'=&gt; DIR 。 '/../view/application/test/index.phtml'到view_manager中的template_map数组。

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

当我访问http://localhost:8080/application/test时,我收到错误消息: test(解析为无效的控制器类或别名:test)

我显然做错了什么,但是关于Zend的文档根本不是新手友好的,而且变得非常令人沮丧。有人能指出我正确的方向吗?有这么多的配置,我敢肯定我错过了一些小东西。谢谢!

1 个答案:

答案 0 :(得分:1)

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

这意味着网址为

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

所以visting

/application/test

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

  

解析为无效的控制器类或别名:test

要解决此问题,我强烈反对使用a :controller路由参数,而是使用路径控制器操作。

'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', 
                    ],
                ],
            ],

        ],
    ],
],