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