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



我正在使用zf2骨架应用。
要在现有模块中创建一个新的控制器,我修改了 Module.config.php 文件,例如:

<?php
return array(
'controllers' => array(
    'invokables' => array(
        'AlbumControllerAlbum' => 'AlbumControllerAlbumController', // WORKING FINE
        'AlbumControllerPhoto' => 'AlbumControllerPhotoController', // 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' => 'AlbumControllerAlbum',
                    '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' => 'AlbumControllerPhoto',
                    'action'     => 'index',
                ),
            ),
        ),
    ),
),
'view_manager' => array(
    'template_path_stack' => array(
        'album' => __DIR__ . '/../view',
    ),
),
);`

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

此链接(http://domainname.com/dummy/zend/zf2-tutorial/public/public/相册/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(
        'AlbumControllerAlbum' => 'AlbumControllerAlbumController', // WORKING FINE
        'AlbumControllerPhoto' => 'AlbumControllerPhotoController', // 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' => 'AlbumControllerAlbum',
                    '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' => 'AlbumControllerPhoto',
                    'action'     => 'index',
                ),
            ),
        ),
    ),
),
'view_manager' => array(
    'template_path_stack' => array(
        'album' => __DIR__ . '/../view',
    ),
),
);

最新更新