Scott Keck-Warren
Scott Keck-Warren

Reputation: 1952

Switch Controller and Action Inside of Controller in Zend Framework 1

I'm working on a website using Zend Framework 1 that requires a login to have access and have created a controller name UserController with an action named loginAction to facilitate the logins. Currently, I have the system setup so it redirects the user to /user/login if they attempt to access the site and they aren't logged in but this changes the URL and we doesn't want this to happen. This has brought me to the point where I'm trying to switch the controller inside of the controller if the user hasn't logged in. I have this so far:

require_once(APPLICATION_PATH . '/controllers/UserController.php');
$this->_helper->viewRenderer->renderBySpec('login', array('controller'=>'user'));
$controller = new UserController($this->getRequest(),$this->getResponse());
return $controller->loginAction();

This still renders the page I'm trying to hide but it does appear to be calling the loginAction. Does anyone have a better way to do this or how I can it be fixed?

Upvotes: 0

Views: 483

Answers (1)

konradwww
konradwww

Reputation: 691

You can try to call inside your controllers preDispatch method: _forward().

public function preDispatch()
{
    parent::preDispatch();
    if (!is_user_logged) {
        $this->_forward('login','user');
    }
}

Reference: http://framework.zend.com/manual/1.12/en/zend.controller.action.html#zend.controller.action.utilmethods

Upvotes: 3

Related Questions