如何配置ZF2树页面路由?
我有多级页面。例如:
contacts
contacts/office
contacts/office/office1
products
products/category1
products/category1/product1
等等。
我使用一个控制器用于页面,此配置用于路由:
'page' => array(
'type' => 'Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\PageController'
)
),
'priority' => -1000,
'may_terminate' => false,
'child_routes' => array(
'name' => array(
'type' => 'Segment',
'options' => array(
'route' => ':sn1[/:sn2[/:sn3[/:sn4[/:sn5[/:sn6]]]]]',
'constraints' => array(
'sn1' => '[a-zA-Z][a-zA-Z0-9]*',
'sn2' => '[a-zA-Z][a-zA-Z0-9]*',
'sn3' => '[a-zA-Z][a-zA-Z0-9]*',
'sn4' => '[a-zA-Z][a-zA-Z0-9]*',
'sn5' => '[a-zA-Z][a-zA-Z0-9]*',
'sn6' => '[a-zA-Z][a-zA-Z0-9]*'
),
'defaults' => array(
'controller' => 'Application\PageController',
'action' => 'page'
)
)
)
)
)
页面操作:
$params = array('sn1', 'sn2', 'sn3', 'sn4', 'sn5', 'sn6');
$names = array();
foreach ($params as $param) {
if($this->params($param, NULL) === NULL) {
break;
} else {
$names[] = $this->params($param, NULL);
}
}
var_dump($names);
$path = implode('/', $names);
var_dump($path);
http://example.com/contacts/office/office1的结果:
array (size=3)
0 => string 'contacts' (length=8)
1 => string 'office' (length=6)
2 => string 'office1' (length=7)
string 'contacts/office/office1' (length=23)
有没有更好的实施方式?