我试图在一个ZF2项目中创建一个动态路由。它会是"domain.com/companyurl/products"之类的。公司url是动态的。我做到了:
'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');
在这两种情况下,我已经指定了基本路由值。
我希望你明白我的意思。谢谢。您可以在URL帮助器中使用$reuseRouteParams
选项:
$this->url($name, $params, $options,$reuseMatchedParameters);
如果您将此设置为true,它将重用companyUrl
之前的路由匹配值。
你可以在这里的文档中阅读更多。