我有这些我需要工作的路线:
/user
/user/1
/user/1.json
目前我有
'user' => array('key' => 'user', 'controller' => 'user'),
'user/:id' => array('key' => 'user-id', 'controller' => 'user', 'id' => "\d+"),
'user/:id.json' => array('key' => 'user-id-json', 'controller' => 'user', 'id' => "\d+"),
它匹配所有三个 url 的第一条路由。
我试过这样做:
'user' => array('key' => 'user', 'controller' => 'user'),
'user/"\d+"' => array('key' => 'user-id', 'controller' => 'user', 'id' => 1),
'user/"\d+".json' => array('key' => 'user-id-json', 'controller' => 'user', 'id' => 1),
但只有第一条路线仍然匹配
Zend Route url 规则中是否有办法“封装” :id 以便将其挑选出来并允许我在它之后放置 / 以外的内容?
更新
我的引导程序使用:
public function _initRoutes() {
$this->addRoutes(array(
'client' => array('key' => 'client', 'controller' => 'client'),
'client/:id' => array('key' => 'client-id', 'controller' => 'client', 'id' => "\d+"),
'user' => array('key' => 'user', 'controller' => 'user'),
'user/:id' => array('key' => 'user-id', 'controller' => 'user', 'id' => "\d+")
));
}
我的扩展扩展器使用
public function addRoutes($routes) {
$router = Zend_Controller_Front::getInstance()->getRouter();
foreach ($routes as $pattern => $data) {
$router->addRoute(
$data['key'],
new Zend_Controller_Router_Route(
$pattern,
array('controller' => $data['controller'])
)
);
}
}
我试着做 addRoutes 这个:
public function addRoutes($routes) {
$router = Zend_Controller_Front::getInstance()->getRouter();
foreach ($routes as $pattern => $data) {
$router->addRoute(
$data['key'],
new Zend_Controller_Router_Route(
$pattern,
array('controller' => $data['controller'])
)
);
$router->addRoute(
$data['key'].'-json',
new Zend_Controller_Router_Route(
$pattern.'.json',
array('controller' => $data['controller'])
)
);
}
}
但后者例如取消 /user/50