php - zend 框架中一个模块中的多个 Controller

标签 php zend-framework2

嗨,我是 zend framework2.2.0 的新手。我想创建一个带有多个 Controller 的模块 我已经从 github 下载了“Album”模块并且工作正常 现在我想在其中添加更多 Controller 下面我展示了模块中文件的文件夹结构

module/
    Album/
         config/
             module.config.php
         src/
            Album/
                Controller/
                         AlbumController.php
                         UserController.php
               Form/
                     AlbumForm.php
                     UserForm.php
               Model/
                   AlbumTable.php 
                   Album.php
                   UserTable.php 
                   User.php

        view/
            album/ 
                 album/             
                       index.phtml
                 user/             
                       index.phtml

我还更改了文件中的所有 namespace

命名空间Album\Controller;

类 UserController 扩展\Zend\Mvc\Controller\AbstractActionController

一些 indexAction 方法返回一个新的\Zend\View\Model\ViewModel();

然后你可以创建你的 View 文件

相册/ View /相册/用户/index.phtml 我做了以上更改。 “Album/Module.php”文件中是否需要任何更改? 你能告诉我通过哪个链接可以看到用户列表吗?

我厌倦了这个错误帮助我摆脱它

enter image description here

最佳答案

您可能需要在 Album/config 文件夹中的 module.config.php 中为用户添加 url 规则。然后你可以像这样访问 url

// The following section is new and should be added to your file
'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',
                ),
            ),
        ),
        'user' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/user[/:action][/:id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Album\Controller\User',
                    'action'     => 'index',
                ),
            ),
        ),
    ),
),

还有

'controllers' => array(
    'invokables' => array(
        'Album\Controller\Album' => 'Album\Controller\AlbumController',
        'Album\Controller\User' => 'Album\Controller\UserController',
    ),
),

关于php - zend 框架中一个模块中的多个 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16932781/

相关文章:

php - com_create_guid() 未定义 : returns fatal error

php - 什么更快,选择多行或多列?

php - 如何打开没有后缀的附件文件?

zend-framework2 - Zend Framework 2 - 身份验证/ACL

PHP session 似乎无法正常工作

php - fatal error : Call to undefined function imap_open() in PHP

zend-framework2 - ZF2 : Redis | Change dump. rdb 位置

zend-framework2 - Zend Framework 2 的 gitignore 文件有什么好的例子吗

php - Zend/Barcode/Barcode.php 处的 Codeigniter + Zend 条码错误

php - ZF2,添加多个表