URL和$ this-> url中的Zf2语言环境 [英] Zf2 Locale in URL and $this->url

查看:90
本文介绍了URL和$ this-> url中的Zf2语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing a web app on ZF2 Skeleton App base. I have played with lot of options but failed to get final headway.

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

I need to route the url as under:

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

到AlbumController/indexAction.此外,链接的工作方式为:

to AlbumController/indexAction. Also, links need to work as:

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

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

我的Application/module.config.php如下:

My Application/module.config.php is as under:

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' 
                ) 
        ) 
);


我的Album/module.config.php具有以下路由器:


My Album/module.config.php has the following router:

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


//////////////////////////////////////////////////////////////////////////////////////////// //现在可以正常工作了.


//////////////////////////////////////////////////////////////////////////////////////// // Now this works fine.

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

Also, when I do call the $this->url('album',array('action'=>'edit', 'id' => $album->id)); in view file (.phtml), it does'nt return proper url as expected:

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

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

//////////////////////////////////////////////////////////////////////////////////////// //Corrected code works for URL $this->url('album', array('action'=>'edit', 'id' => $album->id)) ///////////////////////////////////////////////////////////////////////////////////////

提前感谢您的帮助.

推荐答案

问题在Album/module.config.php中缺少'/':

The issue was missing '/' in Album/module.config.php:

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

应该是:

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

再次感谢大家的帮助.

这篇关于URL和$ this-> url中的Zf2语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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