本日、cakePHPのAuthComponetで気になったことがあったので記録しておきます。
ログイン後の分岐で下記のように書いたとします。
(そもそもが間違っていますが)
if ($this->Auth->redirect() ='/' || ereg('confirm',$this->redirect()) {
$this->redirect('/home/index');
}
これは正しく動作しません。
何故かというと、コアのソースを読むと分かりますが、
(cake/libs/controller/components/auth.php:665行目)
function redirect($url = null) {
if (!is_null($url)) {
$redir = $url;
$this->Session->write('Auth.redirect', $redir);
} elseif ($this->Session->check('Auth.redirect')) {
$redir = $this->Session->read('Auth.redirect');
$this->Session->delete('Auth.redirect');
if (Router::normalize($redir) == Router::normalize($this->loginAction)) {
$redir = $this->loginRedirect;
}
} else {
$redir = $this->loginRedirect;
}
return Router::normalize($redir);
}
一回目の呼び出しでセッションが消えているのがわかります。
$url = $this->Auth->redirect();
if ($url == '/' || ereg('confirm',$url)) {
$this->redirect = '/home/index';
}
正しくはこうなります。
ただそもそもが下記のような書き方をするので、あまり間違えることが少ないことも承知しておりますw
$url = $this->Auth->redirect();
