to identify if there is a route zend framework 2

46 Views Asked by At

I have a url that redraw $ this->view->url($item['action']) this way but this variable can contain the route in the system or not, how can I do this check, check if this route exists in ZF2 ?

1

There are 1 best solutions below

0
On

One option in my mind is to pass the navigation object to the view

In controller:

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

In view:

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

Another option would be to enclose the call in a try/catch. If the function throws an exception, the route does not exist

try {
    $url = $this->view->url($item['action']);
} catch(Zend\Router\Exception\RuntimeException $e) {
    $url = '';
}