【问题标题】:Issue in Create new Controller in Zend Framework 2 (ZF2)在 Zend Framework 2 (ZF2) 中创建新控制器的问题
【发布时间】:2012-12-24 10:36:38
【问题描述】:

我正在使用 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.

【问题讨论】:

  • 呃,你做的路线是一样的,没办法分开。第二条路线可能应该是'route'=&gt;'/photo[/:action][/:id]',
  • 这反转了情况。现在,第二个链接正常工作,第一个链接显示错误:“发生 404 错误页面未找到。请求的 URL 无法通过路由匹配。”
  • 我正在使用相同的模块(相册)和 2 个控制器(AlbumController.php、PhotoController.php)。是不是有问题?
  • 请查看您的'route' =&gt; '' 声明。两者都链接到相同的路由设置,按照您在此线程中发布的配置进行。如果您更新代码,请更新您的问题
  • 谢谢@Sam。我编辑了问题。但是,现在虽然 PhotoController 开始工作,但 AlbumController 不工作。

标签: php zend-framework2


【解决方案1】:

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

),
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多