3
votes

I am trying to get my head around Module routing in ZF2.

At the moment I am only able to create a single controller for a single action and am struggling to figure this routing out. I have looked at other modules and plugins and I kind of get it, just need a small push to "get it".

In this example I am trying to route to the two actions: indexAction and cmstoolsAction

Essentially a user navigates to:

/affiliates/overview
/affiliates/cmstools

And the error is:

The requested URL could not be matched by routing.

I think where I am struggling is firstly comprehending how MVC works and getting lost in the detail. There is so much information in the manual that it becomes a little overwhelming.

anyway - would greatly appreciate any input!

Image of the Module structure:

Affiliate module

My controller looks like this:

<?php
namespace Affiliates\Controller;
use Zend\Mvc\Controller\AbstractActionController;

class AffiliatesController extends AbstractActionController
{
    //Overview page
    public function IndexAction()
    {

    }

    public function CmstoolsAction()
    {

    }


}

And my module config looks like this:

<?php
return array(
'view_manager' => array(
    'template_path_stack' => array(
        'affiliates' => __DIR__ . '/../view',
    ),
),
'controllers' => array(
    'invokables' => array(
        'Affiliates\Controller\Affiliates' =>
        'Affiliates\Controller\AffiliatesController'
    ),
),
'router' => array(
    'routes' => array(
        'affiliates' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/overview',
                'defaults' => array(
                    'controller'    => 'Affiliates',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'cmstools' => array(
                    'type'    => 'Literal',
                    'options' => array(
                        'route'    => '/cmstools',
                        'defaults' => array(
                            'controller' => 'Affiliates',
                            'action'     => 'cmstools',
                        ),
                    ),
                ),

            ),
        ),

    ),

),

);
1
Note one small mistake in the folder tree. I updated the view directory to be: view/affiliates/affiliates/HappyCoder

1 Answers

2
votes

The routing config is the only important part here. At the moment, you have a route for /overview, which has a child route for /cmstool. This would match the following URLs:

/overview
/overview/cmstool

Not quite what you were after.

There are a few different ways you could configure this. The one closest to what you currently have would be a route for /affiliates, with two child routes, one for each of your actions. The configuration for this would be:

'router' => array(
    'routes' => array(
        'affiliates' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/affiliates',
                'defaults' => array(
                    'controller'    => 'Affiliates',
                    'action'        => 'index',
                ),
            ),
            'child_routes' => array(
                'overview' => array(
                    'type'    => 'Literal',
                    'options' => array(
                        'route'    => '/overview',
                        'defaults' => array(
                            'controller' => 'Affiliates',
                            'action'     => 'index',
                        ),
                    ),
                ),
                'cmstools' => array(
                    'type'    => 'Literal',
                    'options' => array(
                        'route'    => '/cmstools',
                        'defaults' => array(
                            'controller' => 'Affiliates',
                            'action'     => 'cmstools',
                        ),
                    ),
                ),
            ),
        ),
    ),
),

This configuration contains three routes: affiliates, overview and cmstools. The latter two are both child routes of affiliates. Note that I removed the line 'may_terminate' => true, from the affiliates route. This determines whether or not the affiliates route would match on its own (i.e. whether the URL /affiliates would work). Since you didn't list this I'm assuming you don't want it to.

Another way you could configure this would be to simply create two literal routes, once for each URL (not using child routes at all):

'router' => array(
    'routes' => array(
        'overview' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/affiliates/overview',
                'defaults' => array(
                    'controller' => 'Affiliates',
                    'action'     => 'index',
                ),
            ),
        ),
        'cmstools' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/affiliates/cmstools',
                'defaults' => array(
                    'controller' => 'Affiliates',
                    'action'     => 'cmstools',
                ),
            ),
        ),
    ),
),