在 Zend Framework 2 (ZF2) 中创建新控制器的问题

2024-04-29

我正在使用 ZF2 Skeleton 应用程序。
为了在现有模块中创建新控制器,我修改了模块.config.php像这样的文件:

<?php
return array(
'controllers' => array(
    'invokables' => array(
        'Album\Controller\Album' => 'Album\Controller\AlbumController', // WORKING FINE
        'Album\Controller\Photo' => 'Album\Controller\PhotoController', // I ADDED THIS
    ),
),

// The following section Was PREVIOUSLY THERE
'router' => array(
    'routes' => array(
        'album' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/album[/:action][/:id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Album\Controller\Album',
                    'action'     => 'index',
                ),
            ),
        ),

    ),
 ),


// ADDED THIS FOR NEW CONTROLLER 'PhotoController.php' in same Namespace 'Album'
'router' => array(
    'routes' => array(
        'photo' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/photo[/:action][/:id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Album\Controller\Photo',
                    'action'     => 'index',
                ),
            ),
        ),

    ),
),

'view_manager' => array(
    'template_path_stack' => array(
        'album' => __DIR__ . '/../view',
    ),

),
);`

这个链接(http://domainname.com/dummy/zend/zf2-tutorial/public/ http://domainname.com/dummy/zend/zf2-tutorial/public/photo/index)正在按预期工作。

这个链接(http://domainname.com/dummy/zend/zf2-tutorial/public/ http://domainname.com/dummy/zend/zf2-tutorial/public/album/index)无法正常工作并显示以下错误:

A 404 error occurred Page not found. 
The requested URL could not be matched by routing.

问题是你声明router在您的配置中两次,第二个会覆盖第一个,因此仅使用第二个路由。

你的配置文件应该看起来像这样,两条路由都在同一个文件中router宣言

<?php
return array(
'controllers' => array(
    'invokables' => array(
        'Album\Controller\Album' => 'Album\Controller\AlbumController', // WORKING FINE
        'Album\Controller\Photo' => 'Album\Controller\PhotoController', // I ADDED THIS
    ),
),

// The following section Was PREVIOUSLY THERE
'router' => array(
    'routes' => array(
        'album' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/album[/:action][/:id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Album\Controller\Album',
                    'action'     => 'index',
                ),
            ),
        ),
        // This is where your new route goes
        'photo' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/photo[/:action][/:id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Album\Controller\Photo',
                    'action'     => 'index',
                ),
            ),
        ),
    ),
),

'view_manager' => array(
    'template_path_stack' => array(
        'album' => __DIR__ . '/../view',
    ),

),
);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Zend Framework 2 (ZF2) 中创建新控制器的问题 的相关文章

随机推荐

  • “致命错误:调用未定义的函数 socket_create()”,即使在 php.ini 中启用

    我有一个奇怪的 PHP 问题 我在 Windows 7 的 IIS 上运行 PHP 5 当我在 PHP 文件中调用 socket create 时 如下所示 sock socket create AF INET SOCK DGRAM get
  • 如何为任何 exe 创建安装程序?

    我有 NET exe 我想为此创建一个设置 并且在任何登录或重新启动后应自动调用此 exe 当您部署 NET 应用程序时 可以采用多种方法来应对这一挑战 选择安装程序策略时 您必须考虑许多问题 这些包括 我计划如何分发该软件 该软件是否应该
  • 全局变量和静态变量存储在内存段的哪里?

    在我的链接器脚本中 起始地址和大小如下 code start 90400000 code end 90a00000 data start 90b00000 size 3MB bss start 91200000 size 1MB 但是当我声
  • 如何在 Amazon Linux 上安装最新版本的 GDAL?

    我想安装GDAL https gdal org 在运行 Amazon Linux 的 EC2 实例上 我认为它基于 RHEL 6 如果可能的话 我想避免从源代码编译 EPEL Yum 存储库中包含的 GDAL 版本对于我的目的来说太旧了 g
  • Akka 和 ReactiveMongo

    我正在尝试找到在集群工作人员之间共享相同连接池的最佳方法 我有以下结构 Master Actor gt Worker Actors 最多 100 个或更多 gt MongoDB 我想在工作人员和 MongoDB 之间放置reactivemo
  • 如何在 VC++ 中使用 _W64 和 __w64?

    有这样的事情 w64 http msdn microsoft com en us library s04b5w00 VS 71 aspx在 Visual C 9 中 我在尝试将本机 C DLL 移植到 64 位时遇到了它 特别是在crtde
  • JavaFX 中按下按钮的样式

    我有一个Button in my FXML文件 我在下面给它一个样式CSS button fx background color linear gradient ff5400 be1d00 fx background radius 30 f
  • 如何在 WPF 中绑定用户控件作为 DataGridTemplateColumn 失败

    我想使用来自不同程序集的用户控件作为 DataGridTemplateColumn 我已经看过很多例子和问题 比如this https stackoverflow com questions 13956767 binding to cust
  • 将 Microsoft.Office.Interop.Excel.Application 转换为 byte[]

    我正在代码中创建一个excel文件 如下所示 Microsoft Office Interop Excel Application excelFile CreateExcelFile 现在我想将此 excel 文件转换为 byte 而不保存
  • 旋转 ImageView 及其背景而不裁剪

    我已经进行了很多搜索 但找不到解决我的问题的方法 我不能使用android rotation因为我希望这个应用程序与 Android API 11 版本兼容 我的问题与此类似 在 Android 中旋转视图 https stackoverf
  • 如何处理解析器中的错误

    我正在尝试使用解析器来打造更好的用户体验 在幸福的道路上 一切都进展顺利 我似乎无法弄清楚如何处理异常 我的解析器调用一个服务 该服务会访问一个 webapi 项目 一个例子 Foo解析器 resolve route ActivatedRo
  • UML - 关联或聚合(简单的代码片段)

    有多少书自相矛盾 真让我抓狂 Class A class B void UseA A a some say this is an association no reference is held but communication is p
  • REST DELETE 真的是幂等的吗?

    DELETE 应该是幂等的 如果我删除http example com account 123 http example com account 123它将删除该帐户 如果我再次这样做 我会收到 404 错误吗 因为该帐户已不存在 如果我尝
  • SSIS - 删除行

    我是 SSIS 新手 需要这方面的帮助 我找到了一个article http consultingblogs emc com jamiethomson archive 2006 09 12 SSIS 3A00 Checking if a r
  • 我可以在 Mercurial 中进行分支而不克隆存储库吗?

    最近 我开始尝试 Mercurial 因为它总是因其简单性和 正常工作 原则而吸引我 或者至少 其他人总是这么描述它 他们通常也将其描述为 实际上与 git 相同 只有一些你不会注意到的细微变化 只是我发现事实并非如此 我的汞分支有问题 如
  • AppCompat DayNight 主题始终显示为 Light 主题?

    我正在使用 AppCompat 中引入的新 Theme AppCompat DayNight 主题版本23 2 http android developers blogspot com 2016 02 android support lib
  • C#.net 中字符串反转的最快方法

    我目前正在为欧拉问题 4 编写一个快速解决方案 其中必须从两个 3 位数的乘积中找到最大的回文数 要确定一个数字是否是回文 您显然需要将该数字的反转与原始数字进行比较 由于 C 没有内置 String Reverse 方法 那么反转字符串的
  • 传递 Range 参数时出现错误 424

    我是 VBA 新手 在这段非常简单的代码中遇到错误 Sub sub1 Dim myCell As Range Set myCell Sheet1 Cells 2 2 sub2 myCell lt lt lt lt Error 424 End
  • jQuery:如何使用“$”而不是“jQuery”?

    我的网站中有简单的 jQuery 但我不断收到此错误 Uncaught TypeError Property of object object DOMWindow is not a function 仅当我使用 而不是 jQuery 时 才
  • 在 Zend Framework 2 (ZF2) 中创建新控制器的问题

    我正在使用 ZF2 Skeleton 应用程序 为了在现有模块中创建新控制器 我修改了模块 config php像这样的文件