2015-06-17 103 views
1

我想配置我的Zf2應用程序,使多個字符串路由到同一個控制器。例如,www.mysite.com/this和www.mysite.com/that都可以路由到同一個控制器,並且可以使用$ this-> params來捕獲這些數據。我將如何完成這樣的事情?我需要2條單獨的路由聲明嗎?ZF2將多個字符串路由到同一個控制器?

'directory' => [ 
    'type'  => 'Zend\Mvc\Router\Http\Literal', 
    'options' => [ 
      'route'  => '/string1 || /string2 || /string3', 
      'defaults' => [ 
       'controller' => 'Application\Controller\MyController', 
       'action'  => 'index' 
      ], 
     ], 
] 
+1

使兩個獨立的路線,像你提到的 – STLMikey

回答

0

由於Literal route定義創建3種途徑:

'directory1' => [ 
    'type'  => 'Zend\Mvc\Router\Http\Literal', 
    'options' => [ 
      'route'  => '/string1', 
      'defaults' => [ 
       'controller' => 'Application\Controller\MyController', 
       'action'  => 'index', 
      ], 
     ], 
], 
'directory2' => [ 
    'type'  => 'Zend\Mvc\Router\Http\Literal', 
    'options' => [ 
      'route'  => '/string2', 
      'defaults' => [ 
       'controller' => 'Application\Controller\MyController', 
       'action'  => 'index', 
      ], 
     ], 
], 
'directory3' => [ 
    'type'  => 'Zend\Mvc\Router\Http\Literal', 
    'options' => [ 
      'route'  => '/string3', 
      'defaults' => [ 
       'controller' => 'Application\Controller\MyController', 
       'action'  => 'index', 
      ], 
     ], 
], 
1

你可以使用一個Zend\Mvc\Router\Http\Regex路由類型,而不是文字之一,這樣做

'directory' => [ 
    'type'  => 'Zend\Mvc\Router\Http\Regex', 
    'options' => [ 
     'route'  => '/string(?<id>[0-9]+)', 
     'defaults' => [ 
      'controller' => 'Application\Controller\MyController', 
      'action'  => 'index' 
     ], 
    ], 
] 
2

最簡單的辦法國際海事組織是:

 'varcatcher' => [ 
      'type' => 'Segment', 
      'options' => [ 
       'route' => '[/[:tail]]', 
       'defaults' => [ 
        'controller' => '\Application\Controller\Index', 
        'action' => 'catch', 
        'module' => 'Application', 
       ], 
       'constraints' => [ 
        'tail' => '[a-zA-z0-9_-]*' 
       ], 
      ], 
      'may_terminate' => true, 
     ], 

然後處理它在你的行動:

public function catchAction(){ 
    die($this->params()->fromRoute('tail')); 
} 

因爲ZF2路線LIFO。通過先插入並處理任何你需要「捕獲」的情況來處理它可能是最佳的。

後進先出法的提及,是因爲如果你「後」定義路由路由器陣列中,這些將先於包羅萬象的,這似乎是有益的,如果我讀過你的問題的權利。

乾杯! 亞歷