6

My redirect:

public function actionLogout()
{
    Yii::$app->user->logout();

    $cookies = Yii::$app->response->cookies;
    $cookies->remove('isBackendLogin');
    unset($cookies['isBackendLogin']);

    if ( !strpos(Url::current(), 'backend') )
    {
        //POST Method sent
        return $this->redirect(['backend/user/auth/logout']);
    }
    return $this->goHome();
}

but logout require post data-method.

Is there any way to redirect to this page?

miken32
  • 42,008
  • 16
  • 111
  • 154

3 Answers3

0

You can use hidden fields on logout page, and submit the form automaticly with some javascript when the user hits that page.

Edit: Just saw the tag of a framework. Sageth, gave a answer for your framework.

RFLdev
  • 186
  • 9
0

You can pass parameters with the redirect if you add parameters to the url array.

return $this->redirect(['/backend/user/auth/logout', 
                        'key' => 'value',
                        'anotherPostKey' => 'anotherPostValue',
                       ]);
Sageth
  • 1,102
  • 1
  • 15
  • 30
  • 2
    In Your example, key and anotherPostKey will be sent by GET method –  Sep 16 '15 at 09:33
  • Oh, I see. It seems that Yii2 cant redirect with POST, but you have a users global variable if you want to store data with the user session. You want more information about that? – Sageth Sep 16 '15 at 09:40
  • Yes, please. My problem is that i have two separate session for frontend and backend. If i logout in frontend i also must logout in backend. [code]public function actionLogout() { Yii::$app->user->logout(); $cookies = Yii::$app->response->cookies; $cookies->remove('isBackendLogin'); unset($cookies['isBackendLogin']); if ( !strpos(Url::current(), 'backend') ) { // redirect this return $this->redirect_post( Yii::$app->urlManager->createAbsoluteUrl(['/backend/user/auth/logout']), [] ); } return $this->goHome(); }[/code] –  Sep 16 '15 at 10:03
  • Edit your original question with this code to find a better ansewer :D – Sageth Sep 16 '15 at 10:23
0

There are two options to achieve this.

  1. Change the logout method allow rule from POST to GET & POST in access rules
    OR
  2. To redirect with POST method, render a page which will have form with POST method type and action set to logout page. Call the form submit using javascript on page load.
Akshay Vanjare
  • 655
  • 3
  • 10