0
votes

What is wrong in this router:

    'routes' => array(
        'home' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller' => 'Application\Controller\Index',
                    'action'     => 'index',
                ),
            ),
        ),
        'application' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/application',
                'defaults' => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller'    => 'Index',
                    '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(
                        ),
                    ),
                ),
            ),
        ),
        'api'         => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/api',
                'defaults' => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller'    => 'Api',
                    'action'        => 'index',                 
                ),
                'may_terminate' => true,
                'child_routes'  => array(
                    'post' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '/post/:id',
                            'constraints' => array(
                                    'id'     => '[0-9]+',
                            ),
                            'defaults' => array(
                                    'controller' => 'Application\Controller\Api',
                                    'action'     => 'post',
                            ),
                        ),
                    ),  
                    'page' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '/page/:name',
                            'constraints' => array(
                                    'name'     => '[a-zA-Z]+',
                            ),
                            'defaults' => array(
                                    'controller' => 'Application\Controller\Api',
                                    'action'     => 'page',
                            ),
                        ),
                    ),
                ),
            ),
        ),  
    ),

I can't route to http://example.com/api/post/0 and to http://example.com/api/page/pagename ? zend framework 2 response with 404 - page not found - "The requested URL could not be matched by routing". thpse two routes are in router definition.

1
I found a problem, 'may_terminate' should be outside options.RoaK
post as answer pleaseSam
I have to wait 10hours to answer my question. After 10hours it should apear. the difference is in 'api' route - 'may_terminate' should be outside 'options' array, so we have to add ), before 'may_terminate' and delete one ), at the end.RoaK

1 Answers

0
votes

Correct router should look like this:

'routes' => array(
    'home' => array(
        'type' => 'Zend\Mvc\Router\Http\Literal',
        'options' => array(
            'route'    => '/',
            'defaults' => array(
                'controller' => 'Application\Controller\Index',
                'action'     => 'index',
            ),
        ),
    ),
    'application' => array(
        'type'    => 'Literal',
        'options' => array(
            'route'    => '/application',
            'defaults' => array(
                '__NAMESPACE__' => 'Application\Controller',
                'controller'    => 'Index',
                '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(
                    ),
                ),
            ),
        ),
    ),
    'api'         => array(
        'type'    => 'Literal',
        'options' => array(
            'route'    => '/api',
            'defaults' => array(
                '__NAMESPACE__' => 'Application\Controller',
                'controller'    => 'Api',
                'action'        => 'index',                 
            ),
        ),
        'may_terminate' => true,
        'child_routes'  => array(
            'post' => array(
                'type'    => 'Segment',
                'options' => array(
                    'route'    => '/post/:id',
                    'constraints' => array(
                            'id'     => '[0-9]+',
                    ),
                    'defaults' => array(
                            'controller' => 'Application\Controller\Api',
                            'action'     => 'post',
                    ),
                ),
            ),  
            'page' => array(
                'type'    => 'Segment',
                'options' => array(
                    'route'    => '/page/:name',
                    'constraints' => array(
                            'name'     => '[a-zA-Z]+',
                    ),
                    'defaults' => array(
                            'controller' => 'Application\Controller\Api',
                            'action'     => 'page',
                    ),
                ),
            ),
        ),
    ),
),