0

I'm trying to get the current route name to be able to do some logic in the view. I need to retrieve this inside the view, not in the controller. For example, in laravel if I wanted to test for a route I would use Request::is('admin/dashboard') or Route::current()->getName().

I've googled many times but I haven't found this answer anywhere.

nullwriter
  • 785
  • 9
  • 34
  • 2
    There's no default view helper for current route. You can send it from controller or write a view helper. checkout MvcEvent's "matchedRoute" method. – Mehmet SÖĞÜNMEZ Nov 10 '17 at 13:02
  • you can find using this `Zend_Controller_Front::getInstance()->getRequest()->getControllerName();` and `Zend_Controller_Front::getInstance()->getRequest()->getActionName();` – Jitendra Yadav Nov 11 '17 at 09:19
  • I had the same question [How to get matched route name in View - Zend Expressive](https://stackoverflow.com/questions/38078780/how-to-get-matched-route-name-in-view-zend-expressive/38101818) and there is an answer for that. – tasmaniski Dec 08 '17 at 09:35

1 Answers1

2

In zend-expressive you can get the matched route from the route result.

$result = $request->getAttribute(RouteResult::class);
$routeName = $result->getMatchedRouteName();

From your action you can pass it into the view.

Some variables I always use and I inject those on every request by creating a wrapper around the TemplateRenderer.

xtreamwayz
  • 1,285
  • 8
  • 10