简体   繁体   中英

ZendFramework2 routing between modules

I have a SkeletonApplication installed and implemented some controllers into the standard module 'Application'.

That works fine.

But now I want to use a second module and I want to set a route from within the 'Application'-module to the new module for linking it there in a view.

The Second module is named 'Sporttabs'.

In my application.config.php I've set as found in documentation:

 // This should be an array of module namespaces used in the application.
'modules' => array(
    'Application',
    'Sporttabs'

),

In the module 'Application' I set in module.config.php:

'routes' => array(
        'home' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'module'    => 'Application',
                    'controller' => 'Index',
                    'action'     => 'index',
                ),
            ),
        ),

        'fach' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/index[/:action][/:id]',
                'constraints' => array(
                    'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Index',
                    'action'     => 'index'
                ),
            ),
        ),

        'sporttabs' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/sporttabs[/:controller][/:action][/:id]',
                'constraints' => array(
                    'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'module' => 'Sporttabs',
                    'controller' => 'Sporttab',
                    'action'     => 'index'
                ),
            ),
        ),



    ),

),

I tried linking it within index.phtml:

<a href="<?php echo $this->url('sporttabs',array('module' => 'sporttabs','controller' => 'sporttab','action' => 'index'))?>">Sporttabs-Projekt</a>

This doesn't work, I only get /sporttab

Even if I try to do www.myurl.de/sporttabs I don't get into the Sporttabs-Module... (I'm using ZendStudio to generate the ne Module, so I think all file are in right position...)

Can you give me a hint how to do this ?

There is no need to define sporttabs inside your Application config. I suggest you do this inside your Sporttabs' module.config.php file.

This is an example of my /admin route, which is a different module named Admin , which sit next to Application.

'router' => [
        'routes' => [
            'admin' => [
                'type'    => 'Literal',
                'options' => [
                    'route' => '/admin',
                    'defaults' => [
                        '__NAMESPACE__' => 'Admin\Controller',
                        'controller'    => 'Index',
                        'action'        => 'index',
                    ],
                ],
                'may_terminate' => true,
                'child_routes' => [
                    'default' => [
                        'type'    => 'Segment',
                        'options' => [
                            'route'    => '/[:controller[/][:action[/][:id][/page/:page][/search/:search]]]',
                            'constraints' => [
                                'controller' => '[a-zA-Z0-9_-]*',
                                'action'     => '[a-zA-Z0-9_-]*',
                                'search'     => '[a-zA-Z0-9_-]*',
                                'id'         => '[0-9]+',
                                'page'       => '[0-9]+',
                            ],
                            'defaults' => [
                                '__NAMESPACE__' => 'Admin\Controller',
                                'controller'    => 'Index',
                                'action'        => 'index',
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ],

From there i do this:

<?=$this->url("admin/default", ['controller' => "controler_name", "action" => "action_name_from_controller", "id" => id_or_something_else_if_needed])?>

/default is there in order to have access to child routes.

@Stanimir hits the right hint for my solution:

While editing the routing for my application, I must have made a unintentional change in the order of the routes array: The 'may_terminate' and 'child_routes' section moved to a upper level instead of being part of the 'fach'-route.

So I changed the array as follows:

'routes'=> array(

        'fach'=> array(
            'type'      => 'Literal',
            'options'   => array(
                'route'     => '/',
                'defaults'  => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller'    => 'Index',
                    'action'        => 'index',
                ),
            ),


        'may_terminate' => 'true',
        'child_routes'  => array(
            'default'   => array(
                'type'  => 'Segment',
                'options'   => array(
                    'route' => '/[:controller[/][:action[/][:id]]]',
                    'constraints'   => array(
                        'controller'    => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'action'        => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'            => '[0-9]+',
                    ),
                    'defaults'  => array(
                        '__NAMESPACE__' => 'Application\Controller',
                        'controller'    => 'Index',
                        'action'        => 'index',
                    ),
                ),
            ),
        ),
      ),),

As an aftereffect of including the Namespace in the routing, I also had to change the controllers-array, because the alias name of the controller changed from 'Index' to 'Application\\Controller\\Index':

'controllers' => array(
    'invokables' => array(
        'Application\Controller\Index' => 'Application\Controller\IndexController',

    ),
),

The same error blocked me from getting into the second module, the routes-array was misordered too.

Now the link-solution Stanimir posted in his answer works fine an I get into my new module...

Thanks for your help Stanimir!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM