在zf3中添加新的控制器到已存在的模块
问题描述:
在Zend框架3中,我尝试将新的控制器“ArticleController”添加到已存在的模块City中,但失败了。我张贴屏幕快照,我的文件夹结构和module.config.php。你能解释一下问题是什么?顺便提及,当访问http://0.0.0.0:7000/city在zf3中添加新的控制器到已存在的模块
访问当工作http://0.0.0.0:7000/article
接着,模块\城市\配置\ 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',
],
],
];
非常感谢您回复并抱歉,迟到回复。我尝试添加“控制器”到module.config.php,但失败了。然后,我找到了解决方案。将文章控制器的值添加到City/src/Module.php中的“工厂”中。完成zend框架教程后,我尝试追加新的控制器。教程应用“工厂”,所以它没有成功。 https://docs.zendframework.com/tutorials/getting-started/database-and-models/非常抱歉,穆罕默德。 – hikozuma
我假设你的“控制器”配置就像 [“controllers”=> [“factories”=> [“MyController”=>“MyControllerFactory”]]] 这就是“controllers”配置方式。如果您的控制器没有任何依赖注入,则不需要工厂。所以你会在“invokables”键下注册它。 [“controllers”=> [“invokables”=> [“MyController”]]] –