简体   繁体   中英

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, ie 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM