I tried to understand ZF2 Module Routing, but I have a problem. I would like to list products by period and generate a link for each of the periods (today, this-week, this-month, all). For example:
<a href="<?php echo $this->url('Application', array('action' => 'index', 'period' => 'this-week')); ?>">This week</a>
output: localhost/application/this-week/
When I click on this link, appears this error:
A 404 error occurred
Page not found.
The requested controller was unable to dispatch the request.
this is my module.config:
.....................
'router' => array(
'routes' => array(
'Application' => array(
'type' => 'Segment',
'options' => array(
'route' => '/application/[:action/][:period/]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'period' => '(today|this-week|this-month|all)'
),
'defaults' => array(
'controller' => 'Application\Controller\ApplicationController',
'action' => 'index',
'period' => 'all'
),
),
),
),
), ...........................
Where am I doing wrong? I tried to change:
'period' => '(today|this-week|this-month|all)'
to
'period' => '[a-zA-Z][a-zA-Z0-9_-]*'
but it doesn't work again.
inside my controller, index action is empty actually:
public function indexAction(){
}
index.phtml contains only a echo link:
<a href="<?php echo $this->url('Application', array('action' => 'index', 'period' => 'this-week')); ?>">This week</a>
NEW EDIT
I've decided to using query string for my use. It's more simple to implement, it isn't elegant but is moooore simple :D ...I thought like so, with a switch for "higher rigidity":
public function indexAction(){
...getQuery method ...
switch($period){
case'today':
.......
break;
case'this-week':
.......
break;
case'this-month':
.......
break;
default:
// if query string is null or contains other characters, sets default case "all"
...
break;
}
}
url function:
<a href="<?php echo $this->url('Application', array('action' => 'index'), array('query' => array('period' => 'this-week'); ?>">This week</a>
output:
localhost/adopted/?period=this-week
inside module.config:
.....................
'router' => array(
'routes' => array(
'Application' => array(
'type' => 'Segment',
'options' => array(
'route' => '/application/[:action/]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Application\Controller\ApplicationController',
'action' => 'index',
),
),
),
),
), ...........................