Zend routing

I’m trying to set up routes, but I’m missing something somewhere.

Here’s my module.config.php file:


<?php
return array(
    'controllers' => array(
        'invokables' => array(
            'Stock\\Controller\\Stock' => 'Stock\\Controller\\StockController',
            'Stock\\Controller\\Media' => 'Stock\\Controller\\MediaController',
        ),
    ),
    'router' => array(
        'routes' => array(
            'stock' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/stock[/][:model][/:id]',
                    'constraints' => array(
                        'model' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'    => '[0-9]*',
                    ),
                    'defaults' => array(
                        'controller' => 'Stock\\Controller\\Stock',
                        'action'     => 'index',
                    ),
                ),
            ),
            'media' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'       => '/stock/media[/:action]',
                    'constraints' => array(
                        'action'          => '[a-zA-Z0-9]*',
                    ),
                    'defaults' => array(
                        'controller' => 'Stock\\Controller\\Media',
                        'action'     => 'index',
                    ),
                ),
            ),
        ),
    ),
    'view_manager' => array(
        'template_path_stack' => array(
            'orders' => __DIR__ . '/../view',
        ),
    ),
);

And my StockController.php file:


private $apiRoutes = array(
        'categories'     => 'catalog/category',
        'certificates'     => 'catalog/certificate',
        'colours'         => 'catalog/color',
        'departments'     => 'catalog/department',
        'designs'         => 'catalog/design',
        'discounts'     => 'sales/discount',
        'locations'     => 'catalog/location',
        'materials'     => 'catalog/material',
        'patterns'         => 'catalog/pattern',
        'products'         => 'catalog/product',
        'ranges'         => 'catalog/range',
        'suppliers'     => 'catalog/supplier' 
    );

public function indexAction()
{

    $model = $this->params()->fromRoute('model') ? $this->params()->fromRoute('model') : 'index';

    $modelForm = 'Stock\\Form\\\\' . ucfirst($model) . 'Form';

    $form = new $modelForm();
    $API = new Application();

    $itemId = $this->params()->fromRoute('id') ? $this->params()->fromRoute('id') : null;

    $dataObj = $API->get($this->apiRoutes[$model]);

        var_dump($dataObj); //outputs $dataObj here, so this method is being called successfully

    foreach($dataObj as $data){

        $vars = get_object_vars($data);

        if($data->{key($vars)} == $itemId){
            $form->setData($vars);
        }
    }

    $formControllerResponse = $this->validate($form);

    if($formControllerResponse->valid){
        //update database
    }

    $view = new ViewModel(array('form' => $form, 'dataObj' => $dataObj, 'itemId' => $itemId));
    $view->setTemplate("stock/stock/$model"); //file zend/module/Stock/view/stock/stock/colours.phtml exists

    return $view; 
}

public function validate($form)
{
    //handle for validation etc...
}

If I navigate to /stock/colours for example I get the following Zend\Mvc\Router\Exception\RuntimeException:

Route with name “colours” not found

If I add a ‘colours’ route to my module.config.php file, something like:


<?php
return array(
    'controllers' => array(
        'invokables' => array(
            'Stock\\Controller\\Stock' => 'Stock\\Controller\\StockController',
            'Stock\\Controller\\Media' => 'Stock\\Controller\\MediaController',
        ),
    ),
    'router' => array(
        'routes' => array(
            'stock' => array(
                ...
            ),
            'media' => array(
                ...
            ),
[B]        'colours' => array(
                ...
            ),[/B]
        ),
    ),
    'view_manager' => array(
        'template_path_stack' => array(
            'orders' => __DIR__ . '/../view',
        ),
    ),
);

It works fine, but I don’t want to do that, because essentially everything’s the same, its just the view template needs to change every time.

Can anyone see what I’m missing…?

Thanks in advance,
Mike