php - 在 Zend Framework 2 (ZF2) 中创建新 Controller 的问题

标签 php zend-framework2

我正在使用 ZF2 Skeleton 应用程序。
为了在现有模块中创建新 Controller ,我修改了 module.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/ 照片/index)按预期工作。

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

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

最佳答案

问题是您在配置中声明了 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',
    ),

),
);

关于php - 在 Zend Framework 2 (ZF2) 中创建新 Controller 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14020170/

相关文章:

php - 将表单字段设置为有条件只读 (PHP)

php - 使用 IN 从结果集中选择

php - 需要来自 Zend Paginator 对象的动态列信息

php - 授予 bjyauthorize 从 CLI 运行 ZF2 的 mvc 应用程序的权限

php - 使用 Laravel Blade 显示来自 Postgres 的 JSON 数据

php - 如何选择所有条件为空的地方?

symfony - 使用 for 循环表示 Twig 中的变量

php - 无法延长 ZF2 中的 session 超时

php - 使用 union 创建 zf2 查询 'not in' 子句

PHP PDO 异常