0

我正在尝试学习Zend Framework 2,并将其骨架应用程序启动并运行。为了访问它,我访问http://localhost:8080/。当访问该链接时,它会显示其通用Zend页面。我想要能够做的是访问http://localhost:8080/application/test,并让我带着另一个不同版面的页面。Zend Framework 2路由错误:解析为无效的控制器类别或别名

这里是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(
      ), 
     ), 
    ), 
); 

为了让这对我来说工作是我的尝试:

首先,我创建了一个名为中的TestController应用/ src目录/应用控制器/控制器/。

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

我还向控制器阵列添加了'Application \ Controller \ Test'=>'Application \ Controller \ TestController'。

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

我明明做错事,但TBH对Zend的文件是不是福利局友好的一切,它变得非常令人沮丧。有人能指出我正确的方向吗?有这么多的配置,我确信我错过了一些小东西。谢谢!

回答

1

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

这意味着该网址会

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

所以visting

/application/test 

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

Resolves to invalid controller class or alias : test

要解决这一点,我会强烈建议更换反对使用一个: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', 
        ], 
       ], 
      ], 

     ], 
    ], 
], 
+0

我的上帝,你是一个圣人。非常感谢。 – cantread