Zend Framework 2路由错误:解析为无效的控制器类或别名 [英] Zend Framework 2 routing error: resolves to invalid controller class or alias

查看:107
本文介绍了Zend Framework 2路由错误:解析为无效的控制器类或别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习Zend Framework 2,并且已经启动并运行了它们的框架应用程序。为了访问它,我访问了 http:// localhost:8080 / 。访问该链接时,它将显示其常规的Zend页面。我想做的是访问 http:// localhost:8080 / application / test 并获得

I'm trying to learn Zend Framework 2 and I have their skeleton application up and running. In order to access it I visit 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 and have it bring me to a different page with a different layout.

这是module.config.php

Here is the 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(
            ),
        ),
    ),
);

为了使它对我有用,这就是我尝试的方法:

In order to make it work for me this is what I tried:

首先,我在Application / src / Application / Controller /中创建了一个名为TestController的控制器。

First I created a controller named TestController in Application/src/Application/Controller/.

接下来,我添加了 application / test / index => DIR 。 '/../view/application/test/index.phtml'到view_manager中的template_map数组。

Next I added 'application/test/index' => DIR . '/../view/application/test/index.phtml' to the template_map array in view_manager.

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

I also added 'Application\Controller\Test' => 'Application\Controller\TestController' to the controllers array.

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

When I visit http://localhost:8080/application/test I get the error: test(resolves to invalid controller class or alias: test)

我显然做错了什么,但是Zend上的文档根本不是newb友好的,这变得非常令人沮丧。有人可以指出我正确的方向吗?配置太多了,我肯定会遗漏一些小东西。谢谢!

I'm obviously doing something wrong but tbh the documentation on Zend is not newb friendly at all and it's becoming very frustrating. Could someone point me in the correct direction? There's just so much configuration that I'm sure I'm missing something small. Thanks!

推荐答案

此刻,名为 application 的路由(父)定义了 / application 的URL路由。子路由 default 但是要求将控制器名称作为第一个参数传递,随后是
操作。

At the moment the route named application (the parent) defines a URL route of /application. The child route default however requires the controller name to be passed in as the first argument, followed by action.

这表示网址将是

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

如此访问

/application/test

您无意间试图获取测试控制器;

You are inadvertently trying to fetch the 'test' controller; hence the error.


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

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

To resolve this I would strongly recomend against using the a :controller route parameter but instead use a route per controller action.

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

        ],
    ],
],

这篇关于Zend Framework 2路由错误:解析为无效的控制器类或别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆