Search code examples
zend-frameworkzend-auth

Best way to redirect when logged in


I'm looking to create an index page of example.com that shows a login form when an user first goes on the site. Then after logging in, example.com will show a main page of their content. What is the best practice of accomplishing this in Zend Framework?

Would it be?

//example.com
//if auth is false, show a login form

//else...
$auth = Zend_Auth::getInstance();
if($auth->hasIdentity()) $loggedIn = true;

//show "Hello World"

So basically like "if you are signed in, echo "Hello World", else show login form". I'm trying to limit the amount of if, else statements and was wondering if this was the only way to go.


Solution

  • Perhaps try rendering different views: http://framework.zend.com/manual/1.11/en/zend.controller.action.html#zend.controller.action.viewintegration.render

    Not tried this myself, but:

    $auth = Zend_Auth::getInstance();
    if (!$auth->hasIdentity()) {
        //login.phtml
        $this->render('login');
    } else {
        //content.phtml
        $this->render('content');
    }
    

    Not sure if there's a way of doing that without the if-else, are they really that big a problem? Controller actions should be fairly simple as far as I know, so there shouldn't be a huge amount of nesting around this code, or cyclomatic complexity within an action function?