2015-10-02 140 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

这真的解决了我的问题。非常感谢! –