php - Zend 框架无法路由翻译后的子路由

标签 php zend-framework2 zend-route zend-translate zend-framework3

我是 Zend Framework 3 的新手,正在尝试翻译路线,我已经部分成功了。我能够翻译主路线并将其重定向到所需的位置,但对于子路线,翻译有效但重定向无效'吨。你能帮帮我吗,我的代码在下面。

模块.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',
                                ],
                            ],  
                        ],
                 ],
            ],
        ],
    ],

模块.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

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

根据我上面的代码,我可以使用路由“language.devgrafioffshore.com/de/Einstellung”重定向到设置 Controller

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

路由无法匹配请求的 URL。

提前致谢。 再见!

最佳答案

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

设置/添加/添加

设置/添加/测试

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.

关于php - Zend 框架无法路由翻译后的子路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38627013/

相关文章:

php - 自动刷新页面

php - Symfony2 中的 CollectionType 验证

php - Zend 框架 2 : Set options as "selected" in select list

php - ZF2 : Custom user mapper for ZfcUser module

php - 如何将 Controller /操作设置为找不到页面

php - 转换 Zend_Rest_Route 代码来处理 2 个参数?

php - 如何使用PHP检查数据是否已经在数据库中?

PHP:类名常量与字符串性能

php - 三元运算符在 godaddy php 中不起作用(parsererror)