Get route match
public getRouteMatch ( ) : null | Zend\Router\RouteMatch | ||
return | null | Zend\Router\RouteMatch |
public function onDispatch(MvcEvent $e) { $routeMatch = $e->getRouteMatch(); $controller = $routeMatch->getParam('controller'); $action = $routeMatch->getParam('action'); // Do something with controller and action }
public function onBootstrap(MvcEvent $e) { $application = $e->getApplication(); $eventManager = $application->getEventManager(); $eventManager->attach(MvcEvent::EVENT_ROUTE, array($this, 'checkAuth')); } public function checkAuth(MvcEvent $e) { $routeMatch = $e->getRouteMatch(); $routeName = $routeMatch->getMatchedRouteName(); if ($routeName !== 'login' && !$this->userIsAuthenticated()) { $response = $e->getResponse(); $response->getHeaders()->addHeaderLine('Location', '/login'); $response->setStatusCode(302); $response->sendHeaders(); return $response; } } private function userIsAuthenticated() { // Check if user is authenticated here }In this example, the onBootstrap method is attaching the checkAuth method to the MvcEvent::EVENT_ROUTE event. The checkAuth method then retrieves the currently matched route and checks if the user is authenticated. If the user is not authenticated and the current route is not 'login', the response headers are modified to redirect the user to the login page. Package library: zend-mvc
public getRouteMatch ( ) : null | Zend\Router\RouteMatch | ||
return | null | Zend\Router\RouteMatch |