简体   繁体   中英

zend framework 2 not matched by routing

I have a couple of routes that don't work and i don't understand why because all the others works, here my module.config.php :

return array(
'controllers' => array(
    'invokables' => array(
        'FrontApp\Controller\Index' => 'FrontApp\Controller\IndexController',
        'FrontApp\Controller\User' => 'FrontApp\Controller\UserController',
    ),
),


'router' => array(
    'routes' => array(
        'confirm' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/confirm',
                'defaults' => array(
                    'controller' => 'FrontApp\Controller\Index',
                    'action'     => 'confirm',
                ),
            ),
        ),
        'user' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/connexion',
                'defaults' => array(
                    'controller' => 'FrontApp\Controller\User',
                    'action'     => 'index',
                ),
            ),
        ),
        'detail' => array(
            'type'    => 'Segment',
            'options' => array(
                'route'    => '/:type/:location/:id',
                'constraints' => array(
                    'type'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'location' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'       => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'FrontApp\Controller\Index',
                    'action'     => 'detail',
                ),
            ),
        ),
        'search' => array(
            'type'    => 'Segment',
            'options' => array(
                'route'    => '/:type[/:location]',
                'constraints' => array(
                    'type'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'location' => '[a-zA-Z][a-zA-Z0-9_-]*',
                ),
                'defaults' => array(
                    'controller' => 'FrontApp\Controller\Index',
                    'action'     => 'search',
                ),
            ),
        ),
        'front' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller' => 'FrontApp\Controller\Index',
                    'action'     => 'index',
                ),
            ),
        ),
    ),
),

'view_manager' => array(
    'display_not_found_reason' => true,
    'display_exceptions'       => true,
    'doctype'                  => 'HTML5',
    'not_found_template'       => 'error/404',
    'exception_template'       => 'error/index',
    'template_map' => array(
        'layout/layout'           => __DIR__ . '/../view/layout/front-layout.phtml',
        'layout/frontlayout'           => __DIR__ . '/../view/layout/front-layout.phtml',
        'frontapp/index/index' => __DIR__ . '/../view/front-app/index/index.phtml',
        'error/404'               => __DIR__ . '/../view/error/404.phtml',
        'error/index'             => __DIR__ . '/../view/error/index.phtml',
    ),
    'template_path_stack' => array(
        'front' => __DIR__ . '/../view',
        'searcher' => __DIR__ . '/../view',
        'detail' => __DIR__ . '/../view',
        'user' => __DIR__ . '/../view',
        'confirm' => __DIR__ . '/../view',
    ),
),
'module_layouts' => array(
        'FrontApp' => 'layout/frontlayout',
        'BackApp' => 'layout/backlayout',
),);

so when i try :

mywebsite/ -> work

mywebsite/one-type -> work

mywebsite/one-type/one-location -> don't work

mywebsite/one-type/one-location/on-id -> don't work

mywebsite/connexion ->work

mywebsite/confirm ->work

I'm little bit confused because its like you can't use different route type. So for the route that don't work i either have "....Unable to render template "layout/layout..." error which is resolve when i add 'layout/layout' with the same path that 'layout/frontlayout' in template_map key (i also don't really understand why but it work, so if you can enlighten me, i will be very thankful), or, a "The requested URL could not be matched by routing." and for this error, i'm stuck because i don't see where is the issue :/

hope you can help :)

here the answer :

'routes' => array(
'search' => array(
    'type'    => 'Segment',
    'options' => array(
        'route'    => '/:type[/:location]',
        'constraints' => array(
            'type'     => '[a-zA-Z][a-zA-Z0-9_-]*',
            'location' => '[a-zA-Z0-9_-]*',
        ),
        'defaults' => array(
            'controller' => 'FrontApp\Controller\Index',
            'action'     => 'search',
        ),
    ),
    'may_terminate' => true,
    'child_routes' => array(
        'detail' => array(
            'type'    => 'Segment',
            'options' => array(
                'route'    => '/:id',
                'constraints' => array(
                    'id' => '[0-9]+',
                ),
                'defaults' => array(
                    'action' => 'detail',
                ),
            ),
        ),
    ),
),
'confirm' => array(
    'type'    => 'Literal',
    'options' => array(
        'route'    => '/confirm',
        'defaults' => array(
            'controller' => 'FrontApp\Controller\Index',
            'action'     => 'confirm',
        ),
    ),
),
'user' => array(
    'type'    => 'Literal',
    'options' => array(
        'route'    => '/connexion',
        'defaults' => array(
            'controller' => 'FrontApp\Controller\User',
            'action'     => 'index',
        ),
    ),
),
'front' => array(
    'type'    => 'Literal',
    'options' => array(
        'route'    => '/',
        'defaults' => array(
            'controller' => 'FrontApp\Controller\Index',
            'action'     => 'index',
        ),
    ),
),

),

Literal routes are pushed later, so i had to make sure they match before segment routes that define the top-level segment.

I combined the "search" and "detail" routes, making "detail" a child route of "search" . This will ensure that the router does not try to match an identifier unless a location is also present. This should speed matching, as well as slim down the definition slightly.

I Changed the location constraints regex to accept word and/or Numbers.

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