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