我从PHP 中获取了建议的解决方案: MVC routing function to parse requested views 的好解决方案?并将其调整为我们的内部指南(再次感谢 Supericy)。
我现在有以下代码:
public function parseRequestedView($url) {
$this->view_requested = explode('/', trim($url, '/'));
// Format: [0] viewpoint, [1] child, [2] action (default: show), [3] reference
$this->view_map = array(
$this->parseViewMapArrayKey('device/:reference:/configurations') => array(0, 2, -1, 1),
$this->parseViewMapArrayKey('device/:reference:') => array(0, -1, -1, 1)
);
foreach ($this->view_map as $this->view_map_transitory => $this->view_map_indices) {
if (preg_match($this->view_map_transitory, $url)) {
foreach ($this->view_map_indices as $this->view_index) {
$this->view_resources[] = $this->view_index > -1 ? $this->view_requested[$this->view_index] : null;
}
return $this->view_resources;
}
}
return false;
}
public function parseViewMapArrayKey($key) {
return '#'.str_replace([":reference:", ":string:"], ["\d+", ".+"], $key).'#';
}
它工作正常,除了一个小“问题”:
当我切换键“device/:reference:”和“device/:reference:/configurations”然后调用 device/:reference:/configurations 的视图时,我只得到 device/:reference 的结果。
例如,http ://ww.foo.com/device/123456/configurations将输出:
Array
(
[0] => device
[1] =>
[2] =>
[3] => 123456
)
这是http://ww.foo.com/device/123456的结果。当我将键改回原来的顺序时,一切都应该是这样的:
Array
(
[0] => device
[1] => configurations
[2] =>
[3] => 123456
)
切换按键时如何反转搜索顺序或使功能输出正确的结果?
我找到了这个PHP Reverse Preg_match。但据我所知,这是一个负的 preg_match 。还是我错了?
提前致谢。