Zend框架无法路由已翻译的子路由 [英] Zend framework not able to route the translated child routes

查看:139
本文介绍了Zend框架无法路由已翻译的子路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Zend框架3的新手,我试图转换路由,但部分成功了.我能够转换主路由并将其重定向到所需的位置,但是对于子路由,翻译是可行的,但重定向不会t.您能帮我吗,我的代码在下面.

I am new to Zend framework 3 and was trying to translate the routes and i have partially succeeded.I am able to translate the main route and redirect it to the required location but for child routes the translation works but the redirection doesn't. can you please help me, my code is below.

module.config.php

module.config.php

'router' => [
        'router_class'           => TranslatorAwareTreeRouteStack::class,
        'routes' => [
            'setting' => [
                'type'    => Segment::class,
                'options' => [
                    'route' => '/{locale}/{setting}',

                    'defaults' => [
                        'locale'     => 'de',
                        'controller' => Controller\SettingController::class,
                        'action'     => 'index',
                    ],
                ],

                'may_terminate'=>true,
                 'child_routes' =>[
                        'add' =>[
                            'type'      =>'Segment',
                            'options'   =>[
                                'route'         =>'/{add}',
                                'defaults'=> [
                                     'controller' => Controller\SettingController::class,
                                     'action'     => 'add',
                                ],
                            ],  
                        ],
                 ],
            ],
        ],
    ],

Module.php

Module.php

   public function onBootstrap(MvcEvent $e)
    {
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);

        $language_session = new Container('language');

        if(!$language_session->offsetExists('lang')){

           $language = 'de_DE';

        } else{

            $language = $language_session->lang.'_'.strtoupper($language_session->lang);
        }

        $translator = $e->getApplication()->getServiceManager()->get('translator');
        $translator->setLocale($language); 
        $translator->addTranslationFile('phparray', __DIR__.'/language/'.$language.'.php', 'default',$language);

        $app      = $e->getTarget();
        $app->getEventManager()->attach('route', array($this, 'onPreRoute'), 100);
    }

    public function onPreRoute($e)
    {
        $app      = $e->getTarget();
        $serviceManager       = $app->getServiceManager();
        $serviceManager->get('router')->setTranslator($serviceManager->get('translator'));
    }

还有我的语言文件de_De.php

And my language file de_De.php

return array(
    'locale'  => 'de',
    'setting' => 'Einstellung',
    'add'     => 'hinzufügen',
);

按照我上面的代码,我可以使用" language.devgrafioffshore.com/de/Einstellung "路由重定向到设置"控制器

As per my above code i am able to redirect to Settings controller with the route "language.devgrafioffshore.com/de/Einstellung"

但无法重定向到 language.devgrafioffshore.com/de/Einstellung/hinzufügen,应该重定向我以添加操作,但我得到

But not able to redirect to language.devgrafioffshore.com/de/Einstellung/hinzufügen which should redirect me to add action but i get

请求的URL不能与路由匹配.

The requested URL could not be matched by routing.

先谢谢您. 再见!

推荐答案

我已经在zend framework2中重写了您的代码,看一看,我将尝试解释

I have rewritten your code in zend framework2, take a look, i will try to expalin

'router' => array(
    'routes' => array(
        'setting' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/setting',
                'defaults' => array(
                    '__NAMESPACE__' => '<<MODULE NAME HERE>>\Controller',
                    'controller'    => 'SettingController',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'add' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '/add[/:action[/:id]]',
                        'constraints' => array(
                            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*'
                        ),
                    ),
                ),

            )

        ),
    ),

通过此配置的可能路由为

The possible routes through this configuration are

设置/添加/anyActionName

设置/添加/添加

设置/添加/测试

第一个添加项不是操作,而是作为操作的路径.第二个添加"是指第二个添加".或"anyActionName";是您要执行的动作.使用此配置的一个更好的事情是,您还可以通过URL传递带有动作名称的ID,但是如果您不传递任何ID,就可以了.

The first add is not an action, rather it acts as a path to action. The second "add" or "anyActionName" are the actions you want to execute. A much better thing with this configuration is, that you can pass ID's also with action name through URL, but if you don't pass any ID, it's alright.

另一项非常重要的事情是,以这种方式定义的配置有助于避免定义每个动作名称,因为使用

One more very important thing, Configuration defined this way helps not to define every Action name because with

'constraints' => array(
                            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*'
                        ),

此对动作名称的限制,可以传递任何动作名称,除非该动作名称包含任何特殊字符.如果您对我的解决方案有任何疑问,请随时提问.

this constraint on action name, any Action name could passed, unless the Action Name contains any special character. If you have any questions regarding my solution, feel free to ask.

这篇关于Zend框架无法路由已翻译的子路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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