7

リファラーページに戻る際に問題があります:

first url: http://site.com/venue/apartments/1564/venue-name
referer url: null

このページでは、内部の内容を編集できるのでlogin、ログイン ページに移動するボタンがあります。ログインに成功したら、first url.

second url: http://site.com/users/login
referer url: http://site.com/venue/apartments/1564/venue-name

ログインしようとすると、CakePHP のアクション ルールが原因で、ログイン ページを更新して資格情報を確認する必要problemがあります。

third url: http://site.com/users/login
referer url: http://site.com/users/login

loginページを更新し、コントローラーのアクションで資格情報を確認すると、users以前と同じページを参照するようになり、first url.

loginアクションページにいないかどうかを確認するAppControllerにセッション変数を設定し、これを行うことで、いくつかの解決策を試しました。

AppController.php

public function beforeFilter () {
    $this->setRedirect();
}

public function setRedirect () {
    if ($this->request->params['controller'] != 'users' && $this->request->params['action'] != 'login') {
        $this->Session->write('redir', array('controller'=>$this->request->params['controller'], 'action'=>$this->request->params['action'], implode($this->request->params['pass'], '/')));
    }
}

セッション変数をdebug($this->Session->write('redir'));すべてでテストすると、アクションに行くまで完璧に動作するように見えますlogin:

UsersController.php

public function login () {
    if ($this->request->is ('post')){
        if ($this->Auth->login()) {
            $redir = $this->Session->read('redir');
            if (!empty($redir)) {
                $this->redirect ($redir);
            } else {
                $this->redirect ($this->Auth->redirect());
            }
        }
    }
}

これを行うとアプリケーションが壊れ、debug($redir);var の場合は次のようになります。

array(
    'controller' => 'js',
    'action' => 'jquery',
    (int) 0 => 'plugin/jquery.validata.1.7.min' // just a jquery plugin loaded in default.ctp
)

それ以外の

array(
    'controller' => 'venue',
    'action' => 'apartments',
    (int) 0 => '1564/venue-name'
)

サイト全体の他のビューでdebug($redir);は、すべてがうまく機能し、適切なデータが得られます。

アクションのためのある種のセッション保護ルーチンを考えまし$this->Auth->login()たが、それは私にとってまったくナンセンスです。

ログインしたユーザーを、表示ページではない最後のページにリダイレクトするにはどうすればよいloginですか?

4

3 に答える 3

11

セッションを使用して、最後にアクセスした URL を保持し、ログインが成功すると、ユーザーはそれにリダイレクトされます。

最初に AppController.php で、ログインとログアウトのアクションを除いて、URL をセッションに保存します。

public function beforeFilter() {
    $url = Router::url(NULL, true); //complete url
    if (!preg_match('/login|logout/i', $url)){
        $this->Session->write('lastUrl', $url);
    }
}

ログインアクションの UserController.php には、次のコードがあります。

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            if ($this->Session->read('lastUrl')){
                $this->redirect($this->Session->read('lastUrl'));
                exit;
            }else{
                return $this->redirect($this->Auth->redirect());
            }
        }
        $this->Session->setFlash(__('Invalid username or password'));
    }
}
于 2013-10-10T17:55:55.550 に答える
0

私は次のようにします:

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            $this->redirect($this->Session->read('referer'));
        } else {
            $this->Session->setFlash(__('Não foi possível efetuar o login. Usuário ou senha inválidos'), 'default', array('class' => 'alert alert-danger'));
        }
    } else {
         $this->Session->write('referer', $this->referer());
    }
}
于 2015-11-26T11:34:18.160 に答える