ZF2:具有不同约束的控制器中的多个动作

I would like to make Module Api that have multiple of actions in which paramateres in URL are different - and i would like to check their constraints.

If in module.config.php I make something like below this controller/module takes control of all routes in application.

For example if i try to run http://example.com/notapi it will generate error without zf2 layout because it tries to handle this controller, but when I remove this module from application config it will handle error in ZF2 layout.

What is wrong with this controller?

return array(
'controllers' => array(
    'invokables' => array(
        'Api\Controller\Api' => 'Api\Controller\ApiController',     
    ),
),
'router' => array(
    'routes' => array(
        'api' => array(
            'type'    => 'literal',
            'options' => array(
                'route'    => '/api',
                'defaults' => array(
                    'controller' => 'Api\Controller\Api',
                    'action'     => 'index',
                ),
            ),
        ),
        'action1' => array(
                'type'    => 'segment',
                'options' => array(
                        'route'    => '/api/action1/:param',
                        'constraints' => array(
                                'param'     => '[0-9]+',
                        ),
                        'defaults' => array(
                                'controller' => 'Api\Controller\Api',
                                'action'     => 'action1',
                        ),
                ),
        ),
        'action2' => array(
                'type'    => 'segment',
                'options' => array(
                        'route'    => '/api/action2/:type/:lang',
                        'constraints' => array(
                                'type'   => '[012]',
                                'lang'   => 'pl|by|ru|ua',
                        ),
                        'defaults' => array(
                                'controller' => 'Api\Controller\Api',
                                'action'     => 'action2',
                        ),
                ),
        ),                                                                          
    ),
),
'view_manager' => array(
    'template_path_stack' => array(
        'api' => __DIR__ . '/../view'
    ),
    'strategies' => array(
            'ViewJsonStrategy',
    ),          
),  

);

This my Module.php in Api module

namespace Api;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\View\Model\JsonModel;

class Module 
{
public function onBootstrap(MvcEvent $e)
{
    $eventManager        = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);

    $eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'onDispatchError'), 0);
    $eventManager->attach(MvcEvent::EVENT_RENDER_ERROR, array($this, 'onRenderError'), 0);
}

public function onDispatchError($e)
{
    return $this->getJsonModelError($e);
}

public function onRenderError($e)
{
    return $this->getJsonModelError($e);
}

public function getJsonModelError($e)
{
    $error = $e->getError();
    if (!$error) {
        return;
    }

    $response = $e->getResponse();
    $exception = $e->getParam('exception');
    $exceptionJson = array();
    if ($exception) {
        $exceptionJson = array(
                'class' => get_class($exception),
                'file' => $exception->getFile(),
                'line' => $exception->getLine(),
                'message' => $exception->getMessage(),
                'stacktrace' => $exception->getTraceAsString()
        );
    }

    $errorJson = array(
            'message'   => 'An error occurred during execution; please try again later.',
            'error'     => $error,
            'exception' => $exceptionJson,
    );
    if ($error == 'error-router-no-match') {
        $errorJson['message'] = 'Resource not found.';
    }

    $model = new JsonModel(array('errors' => array($errorJson)));

    $e->setResult($model);

    return $model;
}

public function getConfig()
{
    return include __DIR__ . '/config/module.config.php';
}

public function getAutoloaderConfig()
{
    return array(
            'Zend\Loader\StandardAutoloader' => array(
                    'namespaces' => array(
                            __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                    ),
            ),
    );
}   
}

ZF2 Routes are a "first match" principle. the first route which match will be used. so your first route is /api this will match for all your /api* routes. so your other routes never are used.

return array(
'controllers' => array(
    'invokables' => array(
        'Api\Controller\Api' => 'Api\Controller\ApiController',     
    ),
),
'router' => array(
    'routes' => array(
        'action1' => array(
                'type'    => 'segment',
                'options' => array(
                        'route'    => '/api/action1/:param',
                        'constraints' => array(
                                'param'     => '[0-9]+',
                        ),
                        'defaults' => array(
                                'controller' => 'Api\Controller\Api',
                                'action'     => 'action1',
                        ),
                ),
        ),
        'action2' => array(
                'type'    => 'segment',
                'options' => array(
                        'route'    => '/api/action2/:type/:lang',
                        'constraints' => array(
                                'type'   => '[012]',
                                'lang'   => 'pl|by|ru|ua',
                        ),
                        'defaults' => array(
                                'controller' => 'Api\Controller\Api',
                                'action'     => 'action2',
                        ),
                ),
        ),  
        'api' => array(
            'type'    => 'literal',
            'options' => array(
                'route'    => '/api',
                'defaults' => array(
                    'controller' => 'Api\Controller\Api',
                    'action'     => 'index',
                ),
            ),
        ),                                                                        
    ),
),
'view_manager' => array(
    'template_path_stack' => array(
        'api' => __DIR__ . '/../view'
    ),
    'strategies' => array(
            'ViewJsonStrategy',
    ),          
),  
);

EDIT:
I would use more generic routes. see this for an example:

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

this matches a controller and a action, and the childroute with the wildcard allows for all the params i would ever need.