2017-10-09 114 views
-1

在Zend框架3中,我嘗試將新的控制器「ArticleController」添加到已存在的模塊City中,但失敗了。我張貼屏幕快照,我的文件夾結構和module.config.php。你能解釋一下問題是什麼?順便提及,當訪問http://0.0.0.0:7000/city在zf3中添加新的控制器到已存在的模塊

訪問當工作http://0.0.0.0:7000/article enter image description here enter image description here

接着,模塊\城市\配置\ module.config.php代碼以下:

<?php 

namespace City; 

use Zend\Router\Http\Segment; 

return [ 
    'router' => [ 
     'routes' => [ 
      'city' => [ 
       'type' => Segment::class, 
       'options' => [ 
        'route' => '/city[/:action[/:id]]', 
        'constraints' => [ 
         'action' => '[a-zA-Z][a-zA-Z0-9_-]*', 
         'id'  => '[0-9]+', 
        ], 
        'defaults' => [ 
         'controller' => Controller\CityController::class, 
         'action'  => 'index', 
        ], 
       ], 
      ], 
      'article' => [ 
       'type' => Segment::class, 
       'options' => [ 
        'route' => '/article[/:action[/:id]]', 
        'constraints' => [ 
         'action' => '[a-zA-Z][a-zA-Z0-9_-]*', 
         'id'  => '[0-9]+', 
        ], 
        'defaults' => [ 
         'controller' => Controller\ArticleController::class, 
         'action'  => 'index', 
        ], 
       ], 
      ], 
     ], 
    ], 

    'view_manager' => [ 
     'template_path_stack' => [ 
      'city' => __DIR__ . '/../view', 
     ], 
    ], 
]; 

回答

2

錯誤信息是明確。應用程序不知道任何關於您的控制器。您的模塊配置必須在「控制器」鍵下有關於控制器的信息。簽出zend documentation,你會在配置文件中看到「controllers」鍵。

+0

非常感謝您回覆並抱歉,遲到回覆。我嘗試添加「控制器」到module.config.php,但失敗了。然後,我找到了解決方案。將文章控制器的值添加到City/src/Module.php中的「工廠」中。完成zend框架教程後,我嘗試追加新的控制器。教程應用「工廠」,所以它沒有成功。 https://docs.zendframework.com/tutorials/getting-started/database-and-models/非常抱歉,穆罕默德。 – hikozuma

+0

我假設你的「控制器」配置就像 [「controllers」=> [「factories」=> [「MyController」=>「MyControllerFactory」]]] 這就是「controllers」配置方式。如果您的控制器沒有任何依賴注入,則不需要工廠。所以你會在「invokables」鍵下注冊它。 [「controllers」=> [「invokables」=> [「MyController」]]] –

相關問題