我試圖理解ZF2模塊路由,但我有一個問題。我想按期間列出產品,併爲每個期間(今天,本週,本月,全部)生成鏈接。例如:Zend Framework 2路由段錯誤
<a href="<?php echo $this->url('Application', array('action' => 'index', 'period' => 'this-week')); ?>">This week</a>
輸出:本地主機/應用/這周/
當我點擊這個鏈接,會出現此錯誤:
A 404 error occurred
Page not found.
The requested controller was unable to dispatch the request.
這是我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'
),
),
),
),
), ...........................
我在哪裏做錯了?我試圖改變:
'period' => '(today|this-week|this-month|all)'
到
'period' => '[a-zA-Z][a-zA-Z0-9_-]*'
,但它不工作了。
我的控制器內,指數的行動實際上是空的:
public function indexAction(){
}
index.phtml只包含回聲鏈接:
<a href="<?php echo $this->url('Application', array('action' => 'index', 'period' => 'this-week')); ?>">This week</a>
新的編輯
我已經決定使用查詢字符串供我使用。它更容易實現,這是不優雅,但moooore簡單:d ...我想,像這樣,與「更高的剛性」開關:
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功能:
<a href="<?php echo $this->url('Application', array('action' => 'index'), array('query' => array('period' => 'this-week'); ?>">This week</a>
輸出:
localhost/adopted/?period=this-week
內部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',
),
),
),
),
), ...........................
請問,如果你從你的路由配置移除操作的默認值,它的工作? –
如果我刪除默認操作值,它的工作原理!但網址是: localhost/application/index/this-week/ –