Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend framework not able to route the translated child routes

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

'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

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

And my language file de_De.php

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

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

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

The requested URL could not be matched by routing.

Thank you in advance. Bye!

like image 965
khukuri Avatar asked Nov 09 '22 11:11

khukuri


1 Answers

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

setting/add/anyActionName

setting/add/add

setting/add/test

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.

like image 156
Seeker Avatar answered Nov 14 '22 23:11

Seeker