【问题标题】:Creating controller on ZF2在 ZF2 上创建控制器
【发布时间】:2014-01-24 08:41:42
【问题描述】:

我是 Zend Framework 的新用户,我有一个项目要做。问题是我无法将路由配置到第二个控制器。第一个控制器(索引)运行良好,但不是新控制器。

我使用 zftool 创建了模块,因此我有一个名为“Medicaments”的模块。当我尝试使用以下 url localhost/project_zend/public(或 localhost/project_zend/public/medicaments)访问我的项目时,它可以工作,并且我到达了我的 IndexController,indexAction。但是当我尝试访问我的新控制器“ConfigurationController(存在于 /modules/Medicaments/Controller/)时,我到达了 404 页面,说“请求的控制器无法调度请求。”我的观点表明使用的控制器是“药物/控制器/索引”而不是“药物/控制器/配置”。

这是我的路由器配置(在 /modules/Medicaments/config/module.config.php 中)文件:

'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller' => 'Medicaments\Controller\Index',
                    'action'     => 'index',
                ),
            ),
        ),
        'medicaments' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/medicaments[/:action][/:id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    '__NAMESPACE__' => 'Medicaments\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(
                        ),
                    ),
                ),
            ),
        ),
        'configuration' => array(
            'type'    => 'segment',
            'options' => array(
                'route'       => '/configuration[/:action][/:id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller'    => 'Configuration',
                    'action'        => 'index',
                ),
            ),
        ),
    ),
),

感谢您的帮助。

【问题讨论】:

  • 您的网址中无需使用“/public”

标签: zend-framework2


【解决方案1】:

请自行处理official Documentation(此处链接:路由和控制器)。

当您不知道工具在做什么时,请尽量避免使用它们。从长远来看,您只会受到伤害,尤其是当您在 sprint 中事情已经开始变得棘手时。

TL/DR:您必须在controllers-配置中创建一个invokables-条目。

【讨论】:

  • 我反复阅读官方文档。我不是要求你只是为了避免自己搜索。我确实使用了可调用对象,但它不起作用。所以谢谢你的回答,但如果只是为了批评,我认为它没有真正的帮助。
  • 好吧,我花了几个小时终于找到了我的问题的答案......这只是因为“
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 2015-03-24
  • 1970-01-01
  • 1970-01-01
  • 2012-09-06
相关资源
最近更新 更多