我应该如何在菜单栏中创建一个注销按钮';s由视图辅助对象渲染


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

我的模块中有一个通过助手从视图分部创建的sub-v栏。

这是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',
        ),
    ),
),

);

所以我的问题是,我不仅仅想将用户重定向到登录页面,我还想清除他们的会话,然后将他们重定向到登录页。我也不想让登录页面在用户导航到会话时只清除会话(以防他们登录并意外点击后退按钮)。

那么,用我当前的配置来处理这个问题的最佳方法是什么呢?我在想,我可以让我的视图助手在会话中为部分集提供一个标志,登录页面会读取该标志并相应地采取行动——它会在会话中检查"logoffButtonPress"标志或其他什么。但是在视图助手中执行这样的操作合适吗?这在视图辅助对象中可能吗?

为什么不让按钮针对logoutAction

我使用的是BjyAuthoriseZfcUser模块,但清除会话和重定向的过程是相同的。

登录控制器

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();
}

正如Alex所说,这就是实现这一目标的方法。

此代码是正确的

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

因此,您需要将注销操作设置为"mymodule''logoff"路由,清除会话并将用户重定向到登录或主页或您想要的位置。

在登录操作中,您不需要清除会话。