【发布时间】:2013-01-22 16:00:09
【问题描述】:
我正在 ZF2 Skeleton App 基础上开发一个网络应用程序。我玩了很多选择,但未能取得最终进展。
我需要按以下方式路由网址:
http://myapp/
http://myapp/en/album
到 AlbumController/indexAction。此外,链接需要作为:
http://myapp/en/album/edit/1
http://myapp/en/album/delete/1
代码生成正确的网址,但点击返回“404”错误
我的 Application/module.config.php 如下:
return array (
'router' => array (
'routes' => array (
'home' => array (
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array (
'route' => '/',
'defaults' => array (
'controller' => 'Album\Controller\Album',
'action' => 'index',
'lang' => 'en',
)
)
),
'application' => array (
'type' => 'Literal',
'options' => array (
'route' => '/application',
'defaults' => array (
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index'
)
),
'may_terminate' => true,
'child_routes' => array (
'default' => array (
'type' => 'Segment',
'options' => array (
'route' => '[:lang[/album[/:action[/:id]]]]',
'constraints' => array (
'lang' => '[a-z]{2}',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+'
),
'defaults' => array (
'controller' => 'Album\Controller\Album',
'action' => 'index',
'lang' => 'en',
)
)
)
)
)
)
),
'service_manager' => array (
'factories' => array (
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory'
)
),
'translator' => array (
'locale' => 'en_US',
'translation_file_patterns' => array (
array (
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo'
)
)
),
'controllers' => array (
'invokables' => array (
'Application\Controller\Index' => 'Application\Controller\IndexController'
)
),
'view_manager' => array (
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array (
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml'
),
'template_path_stack' => array (
__DIR__ . '/../view'
)
)
);
我的相册/module.config.php 有以下路由器:
'router' => array (
'routes' => array (
'album' => array (
'type' => 'segment',
'options' => array (
'route' => '[:lang[/album[/:action[/:id]]]]',
'constraints' => array (
'lang' => '[a-z]{2}',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+'
),
'defaults' => array (
'controller' => 'Album\Controller\Album',
'action' => 'index',
'lang' => 'en',
)
),
)
)
),
/////////////////////////////////////// ////////////////////////////////// // 现在可以正常工作了。
另外,当我调用 $this->url('album',array('action'=>'edit', 'id' => $album->id));在视图文件 (.phtml) 中,它没有按预期返回正确的 url:
http://www.myapp.com/en/edit/id/1
/////////////////////////////////////// ////////////////////////////////// //更正后的代码适用于 URL $this->url('album', array('action'=>'edit', 'id' => $album->id)) ///////////////////////////////////////// /////////////////////////////////////////p>
提前感谢您的帮助。
【问题讨论】:
-
:album没有default和constraint,而且您拨打$this->url()时也没有分配专辑。实际上我想知道这条路线是否正在被创建 -
我刚刚修复的代码的一个问题是:'route' => '[:locale[/:album[/:action[/:id]]]]', 应该是 'route ' => '[:locale[/album[/:action[/:id]]]]',
-
您希望
edit-items作为您的操作,但您将edit作为参数传递。您还希望/id/1作为最后两个段,但在路由[:locale[/:album[/:action[/:id]]]]中没有文字id部分。它使您的问题分散了很多注意力。你能 1) 创建一个 minimal 工作示例并 2) 表达一个明确的问题吗?那么回答你的问题会很有帮助! -
@Sam/@Jurian:感谢您的帮助。我已经用建议/更正更新了代码。现在默认 URL 工作正常,但 /en/album 和 /en/album/edit/1 等返回“404”错误。路由没有正确解析。关于我哪里出错的任何建议。
标签: locale zend-framework2 zend-locale