Ron Piggott Ron Piggott - 2 months ago 9
PHP Question

ZF2 Routing Error

I am receiving the error "The requested URL could not be matched by routing" for the URL: http://domain.com/blog/entry/august/18/2016/9/6/14

I am being very specific with the routing to help sanitize the input. I want to "define" acceptable input for each constraint. I think my error is some type of a mismatch with the constraints. It isn't obvious, I need a fresh set of eyes:

My routing looks like:

'blog-entry' => [
'type' => 'segment',
'options' => [
'route' => '/blog/entry/[:month_reference]/[:day_reference]/[:year_reference]/[:hour_reference]/[:minute_reference]/[:second_reference]',
'constraints' => [
'month_reference' => '(january|february|march|april|may|june|july|august|september|october|november|december)',
'day_reference' => '[1-31]',
'year_reference' => '[0-9]*',
'hour_reference' => '[0-23]',
'minute_reference' => '[0-59]',
'second_reference' => '[0-9]*',
],
'defaults' => [
'controller' => 'Actsministries\Blog\Controller\Blog',
'action' => 'blogentry'
]
]
],

Answer

Those constrains are regexes. For digits you would use [0-9]. To tell it must be between 1 and 2 digits you use {1,2}. For exactly 4 (year) you would use {4}.

'blog-entry' => [
    'type'    => 'segment',
    'options' => [
        'route'    => '/blog/entry/[:month_reference]/[:day_reference]/[:year_reference]/[:hour_reference]/[:minute_reference]/[:second_reference]',
        'constraints' => [
            'month_reference'  => '(january|february|march|april|may|june|july|august|september|october|november|december)',
            'day_reference'    => '[0-9]{1,2}', // Between 1 and 2 digits
            'year_reference'   => '[0-9]{4}',   // Exactly 4 digits
            'hour_reference'   => '[0-9]{1,2}',
            'minute_reference' => '[0-9]{1,2}',
            'second_reference' => '[0-9]{1,2}',
        ],
        'defaults' => [
            'controller' => 'Actsministries\Blog\Controller\Blog',
            'action'     => 'blogentry'
        ]
    ]
],