dpmrakfbx820320638 2015-06-25 14:53
浏览 71

Zend Framework 2路由段错误

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',
                             ),
                         ),
                     ),
                 ),
             ), ...........................
  • 写回答

4条回答 默认 最新

  • dongzhou4727 2015-06-25 15:30
    关注

    With a path like /application/this-week/, ZF has no way of knowing whether this-week is an action or period, since both are optional and the string matches the constraints for both. To fix this you need to remove this ambiguity. You have a few options:

    Option 1: Add some constraints for action, so that it will only match valid actions, something like:

    'constraints' => array(
        'action' => '(index|foo|bar)',
        'period' => '(today|this-week|this-month|all)'
    ),
    

    Option 2: Make either action or period required, by removing the square brackets from them in your routing config. (If you make index required you will end up with 'index' in your URLs.)

    Option 3: Add a separate route for this URL which doesn't include the action in the path (since you don't want it there), but has some other way of distinguishing it from the application route:

    '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'
                     ),
                 ),
             ),
             'period' => array(
                 'type'    => 'Segment',
                 'options' => array(
                     'route'    => '/application/period/[:period/]',
                     'constraints' => array(
                         'period' => '(today|this-week|this-month|all)'
                     ),
                     'defaults' => array(
                         'controller' => 'Application\Controller\ApplicationController',
                         'action' => 'index',
                         'period' => 'all'
                     ),
                 ),
             ),
         ),
     ), 
    

    You'd also change the URL helper call:

    <a href="<?php echo $this->url('period', array('period' => 'this-week')); ?>">This week</a>
    

    This would give you a URL like /application/period/this-week/.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度