Search code examples
phpauthenticationzend-framework2zfcuser

How to forward to another route at Module.php without redirect?


I wish to show login form at page being accessed without url redirection. Is it possible to forward route at Module.php, i.e. if client accesses /stations/ forward it to "user/login" route? Without external redirect, I just want to see login form at private pages.

public function onBootstrap(MvcEvent $e){
    # .... other
    $eventManager->attach(MvcEvent::EVENT_ROUTE, array($this, 'checkAuth'), -100);
}
public function checkAuth(MvcEvent $e){
    # ... get auth service 
    if (!$auth->hasIdentity()) {
        # smth like this
        $router   = $e->getRouter()->setRoutes(array('zfcuser/login'));
        return $router;
    }
    return;
}

This code throws Uncaught exception 'Zend\Mvc\Router\Exception\RuntimeException' with message 'Could not find prototype with name zfcuser/login'.

So the question is the following: how can I substitute route at Module.php?


Solution

  • The problem has been solved thanks to @samsonasik comment to another question.

    public function checkAuth(MvcEvent $e){
        # ... get auth service 
        if (!$auth->hasIdentity()) {
            $e->getRouteMatch()
                         ->setParam('controller', 'zfcuser')
                         ->setParam('action', 'login');
        }
        return;
    }
    

    So I have changed route without external redirection.