2015-10-02 73 views
0

我正在嘗試在ZF2項目中創建動態路線。它會像「domain.com/companyurl/products」。公司網址是動態的。我做到了:ZF2-動態基礎路線

'company' => array(
    'type' => 'Segment', 
    'options' => array(
     'route' => '[/:company]', 
     'defaults' => array(
      'controller' => 'IndexController', 
      'action'  => 'index', 
     ), 
    ), 
     'may_terminate' => true, 
     'child_routes' => array(
      ... 
    ), 

), 

但我總是必須通過公司參數的路線。

$this->url('company/products', array('company' => 'companyurl')); 

有沒有一些方法可以在運行時指定一個基本路由,比如一個基礎url,那麼所有的路由都會跟着它?事情是這樣的:

$this->url('products'); 

$this->url('company/products'); 

在這兩種情況下,我已經規定的基本路線值。

我希望你明白我的意思。謝謝。

+0

你可以繼承URL視圖助手(Zend \ View \ Helper \ Url)以自動爲所有的URL添加公司URL。 – Ed209

回答

1

有一個$reuseRouteParams選項,您可以在URL幫手使用方法:

$this->url($name, $params, $options,$reuseMatchedParameters); 

如果設置爲true,將重用的companyUrl以前的路線匹配值。

您可以在文檔here中閱讀更多內容。

+0

這真的解決了我的問題。非常感謝! –