2

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

此链接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.
4

1 回答 1

3

问题是你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',
    ),

),
);
于 2012-12-24T12:43:01.053 回答