zend framework2 - URL 和 $this->url 中的 Zf2 Locale



我正在ZF2 Skeleton App base上开发一个Web应用程序。我玩过很多选择,但未能取得最终进展。

我需要按以下方式路由网址:

http://myapp/
http://myapp/en/album

到 AlbumController/indexAction。此外,链接需要工作为:

http://myapp/en/album/edit/1
http://myapp/en/album/delete/1

代码生成正确的 URL,但单击返回"404"错误

My Application/module.config.php如下:


return array (
        'router' => array (
                'routes' => array (
                        'home' => array (
                                'type' => 'ZendMvcRouterHttpLiteral',
                                'options' => array (
                                        'route' => '/',
                                        'defaults' => array (
                                                'controller' => 'AlbumControllerAlbum',
                                                'action' => 'index',
                                                'lang'     => 'en',
                                        ) 
                                ) 
                        ),
                        'application' => array (
                                'type' => 'Literal',
                                'options' => array (
                                        'route' => '/application',
                                        'defaults' => array (
                                                '__NAMESPACE__' => 'ApplicationController',
                                                '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' => 'AlbumControllerAlbum',
                                                                'action' => 'index',
                                                                'lang'     => 'en',
                                                        )
                                                ) 
                                        ) 
                                ) 
                        ) 
                ) 
        ),
        'service_manager' => array (
                'factories' => array (
                        'translator' => 'ZendI18nTranslatorTranslatorServiceFactory' 
                ) 
        ),
        'translator' => array (
                'locale' => 'en_US',
                'translation_file_patterns' => array (
                        array (
                                'type' => 'gettext',
                                'base_dir' => __DIR__ . '/../language',
                                'pattern' => '%s.mo' 
                        ) 
                ) 
        ),
        'controllers' => array (
                'invokables' => array (
                        'ApplicationControllerIndex' => 'ApplicationControllerIndexController' 
                ) 
        ),
        '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' 
                ) 
        ) 
);

My Album/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' => 'AlbumControllerAlbum',
                                                'action' => 'index', 
                                                'lang'     => 'en',
                                        )
                                ),
                        )
                )
        ), 

//

/现在这工作正常。

另外,当我调用 $this->url('album',array('action'=>'edit', 'id' => $album->id)); 在视图文件 (.phtml) 中时,它不会按预期返回正确的 URL:

http://www.myapp.com/en/edit/id/1

//

/更正的代码适用于网址$this->url('album', array('action'=>'edit', 'id' => $album->id))///////////////////////////////////////////////////////////////////////////////////////

提前感谢您的帮助。

Album/

module.config.php中缺少"/"的问题:

'route' => '[:lang[/album[/:action[/:id]]]

应该是:

'route' => '/[:lang[/album[/:action[/:id]]]

再次感谢大家的帮助。

相关内容

  • 没有找到相关文章

最新更新