在 Zend Framework 2 (ZF2) 中创建新控制器中的问题 [英] Issue in Create new Controller in Zend Framework 2 (ZF2)

查看:33
本文介绍了在 Zend Framework 2 (ZF2) 中创建新控制器中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ZF2 Skeleton 应用程序.
为了在现有模块中创建新控制器,我修改了 module.config.php 文件,如下所示:

I am using ZF2 Skeleton app.
To create a new controller in a existing Module, I modified the module.config.php file like this:

<?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) 正常工作.

This link (http://domainname.com/dummy/zend/zf2-tutorial/public/photo/index) is working as expected.

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

This link (http://domainname.com/dummy/zend/zf2-tutorial/public/album/index) is not working and showing the following errors:

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

推荐答案

问题是你在配置中声明了 router 两次,第二个覆盖了第一个,所以只有第二个路由是用过.

The problem is you're declaring router in your config twice, the second one overwrites the first, so only the second route is used.

你的配置文件应该是这样的,两个路由都在同一个 router 声明中

Your config file should look something like this with both routes inside the same router declaration

<?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',
    ),

),
);

这篇关于在 Zend Framework 2 (ZF2) 中创建新控制器中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆