确定是否存在路由zend框架2

问题描述 投票:0回答:1

我有一个用这种方式重绘$ this-> view-> url($ item ['action'])的网址,但是这个变量可以包含系统中的路由,我该怎么做才能检查,检查这条路是否存在在ZF2?

zend-framework-routing
1个回答
0
投票

我想到的一个选择是将导航对象传递给视图

在控制器中:

return [
    'navigation' => $this->navigation,
];

在视图中:

if($this->navigation->findOneBy('route', $item['action'])) {
    $url = $this->view->url($item['action']);
}

另一个选择是将调用包含在try / catch中。如果函数抛出异常,则该路由不存在

try {
    $url = $this->view->url($item['action']);
} catch(Zend\Router\Exception\RuntimeException $e) {
    $url = '';
}
© www.soinside.com 2019 - 2024. All rights reserved.