How should I create a log off button in my menu bar that's rendered by a view helper?

I have a subnav bar in my module created from a view partial via a helper.

Here is the config in module.config.php:

'navigation' => array(
    'default' => array(
        array(
            'label' => 'Create',
            'route' => 'mymodule\Create',
        ),
        array(
            'label' => 'View',
            'route' => 'mymodule\view',
        ),
        array(
            'label' => 'Search',
            'route' => 'mymodule\search',
        ),
        array(
            'label' => 'Log Off',
            'route' => 'mymodule\logoff',
        ),
    ),
),

);

So here is my problem, I don't just want to redirect the user to the login page, I want to clear their session and THEN redirect them to the login page. I also don't want to have the login page just clear the session whenever a user navigates to it (in case they logged in and clicked the back button accidentally).

So what is the best way to handle this with my current config? I was thinking I could have my view helper that renders the partial set a flag in the session which the logon page would read and act accordingly- it would check for a "logoffButtonPress" flag in the session or something. But is it appropriate to do something like this in a view helper? Is this even possible in a view helper?

Answers


Why not just have the button target a logoutAction?

I'm using BjyAuthorise and ZfcUser modules but the process of clearing the session and redirecting is the same.

LoginController

public function logoutAction()
{
    $this->authService->logout();

    $redirect = $this->params('redirect', false);

    if ($redirect) {
        return $this->redirect()->toUrl($redirect);
    }

    return $this->redirect()->toRoute(
        $this->config->getLogoutRedirectRoute()
    );
}

AuthService

public function logout()
{
    $adapter = $this->authService->getAdapter();

    $adapter->resetAdapters();
    $adapter->logoutAdapters();

    $this->authService->clearIdentity();
}

Like Alex says, that is the way to do that.

This code is correct

    array(
        'label' => 'Log Off',
        'route' => 'mymodule\logoff',
    ),

So, you need to set a logoff action to 'mymodule\logoff' route, clear the session and redirect the user to login or home or what you want.

On the login action you dont need to clear session.


Need Your Help

Cannot Access http://<tfs-server>:8080

visual-studio visual-studio-2008 iis tfs tfs2008

I've installed TFS 2008, but I can't seem to access the server. When I try to connect to it in Visual Studio, I can't. If I try by browser on a remote PC, I get a generic page cannot be displayed. ...

Pros and Cons of unit testing with t#

.net unit-testing

I've recently stumbled upon T#. It seems a nice concept but I'm wondering if it's worth switching from nUnit to this? I love the pros but hate the cons so I'm still undecided