2013-02-26 75 views
0

我有一個關於MVC路由的問題。MVC路由和網址視圖幫手

我安裝我的路線在module.config.php這樣的:

'router' => array(
'routes' => array(
    'album' => array(
     'type'    => 'segment', 
     'options' => array(
      'route'    => '/album[/page/:page]', 
      'constraints' => array(
       'page'   => '[0-9]+', 
      ), 
      'defaults' => array(
       'controller' => 'Trade\Controller\Album',          
       'action'     => 'index', 
       'page'       => 1, 
      ), 
     ), 
     'may_terminate' => true, 
     'child_routes' => array(
      'default' => array(
       'type'    => 'segment', 
       'options' => array(
        'route'    => '[/:action][/:id]', 
        'constraints' => array(
         'action' => '[a-zA-Z][a-zA-Z0-9_-]*', 
         'id'     => '[0-9]+', 
        ), 
       ), 
      ), 
     ), 
    ), 

有途徑,如:

/album 
/album/page/2 
/album/edit/3 
/album/add 

在視圖代碼中,我使用:

<a href="<?php echo $this->url('album/default', array('action'=>'edit', 'id' => $album->id));?>">Edit</a> 

其中結果

/album/page/1/edit/2 

這不是我想要的url看起來像。我想要的網址看起來像 /專輯/編輯/ 3

,因爲我沒有提供在url參數數組中的頁碼,我沒有料到默認頁面被拿起。

我相信有一個更智能的方法來設置所需的路由,並會感謝任何指針。

彼得

+0

您是否已經嘗試過''page'=> null'? – Ocramius 2013-02-26 09:47:37

+0

是的,我嘗試過,但沒有奏效。 – PWansch 2013-02-27 06:16:48

回答

0

更改module.config.php路線如下

'router' => array(
    'routes' => array(
     'album' => array(
      'type' => 'Literal', 
      'options' => array(
       'route' => '/album', 
       'defaults' => array(
        'controller' => 'Trade\Controller\Album', 
        'action' => 'index', 
       ), 
      ), 
      'may_terminate' => true, 
      'child_routes' => array(
       'add' => array(
        'type' => 'Literal', 
        'options' => array(
         'route' => '/add', 
         'defaults' => array(
          'action' => 'addalbum', 
         ), 
        ), 
       ), 
       'edit' => array(
        'type' => 'Segment', 
        'options' => array(
         'route' => '/edit[/:id]', 
         'constraints' => array(
          'id' => '[0-9]+', 
         ), 
         'defaults' => array(
          'action' => 'editalbum', 
         ), 
        ), 
       ), 
       'page' => array(
        'type' => 'Segment', 
        'options' => array(
         'route' => '/page[/:id]', 
         'constraints' => array(
          'id' => '[0-9]+', 
         ), 
         'defaults' => array(
          'action' => 'pagealbum', 
         ), 
        ), 
       ), 
      ), 
     ), 
    ), 
), 

現在更改您的視圖腳本的鏈接

<a href="<?php echo $this->url('album/edit', array('id'=>$album->id)); ?>">Edit</a> 

您的路線現在將好的。

+0

謝謝!這是有效的,並且是一個很好的解決方案和實踐,因爲確切的路線是定義的 – PWansch 2013-02-27 06:18:15